user2726975
user2726975

Reputation: 1353

C# wrap text programmatically from excel

I have a c# console program that downloads an .xls file that gets converted to .csv file using

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + sourceFile + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes;\"";

OleDbConnection conn = null;
StreamWriter wrtr = null;
OleDbCommand cmd = null;
OleDbDataAdapter da = null;
try
{
    conn = new OleDbConnection(strConn);
    conn.Open();


    cmd = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", conn);
    cmd.CommandType = CommandType.Text;
    wrtr = new StreamWriter(targetFile);

    da = new OleDbDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

In one of the columns, the text needs to be word wrapped. How can I do that? Data looks like this

"The District of Columbia
ZIP 11101.
"

The column should actually be "The District of Columbia ZIP 11101."

Upvotes: 1

Views: 11755

Answers (2)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56707

Remove the line breaks using something like this:

string noWraps = source.Replace(Environment.NewLine, "");

Upvotes: 1

Haji
Haji

Reputation: 2077

After you supply text you should set the cell's IsTextWrapped style to true

worksheet.Cells[0, 0].Style.IsTextWrapped = true;

Upvotes: 1

Related Questions