C#从Excel以编程方式包装文本 [英] C# wrap text programmatically from excel

查看:68
本文介绍了C#从Excel以编程方式包装文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c#控制台程序,该程序下载一个 .xls 文件,该文件使用

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

哥伦比亚特区
ZIP11101.
"

该列实际上应为哥伦比亚特区ZIP 11101".

推荐答案

使用以下方法删除换行符:

Remove the line breaks using something like this:

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

这篇关于C#从Excel以编程方式包装文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆