简体   繁体   English

C# 以编程方式从 excel 换行文本

[英]C# wrap text programmatically from excel

I have ac# console program that downloads an .xls file that gets converted to .csv file using我有下载的C#控制台程序.xls文件,该文件被转换成.csv文件中使用

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."该列实际上应该是"The District of Columbia ZIP 11101."

提供文本后,您应该将单元格的 IsTextWrapped 样式设置为 true

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

使用以下内容删除换行符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM