如何在Excel epplus C#中给换行符 [英] How can I give line break in excel epplus c#
本文介绍了如何在Excel epplus C#中给换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Epplus 库在Excel中转换 dataTable
。我在前端站点中使用了textarea。其中也有换行符。但是,问题是当我将此文件转换为Excel时,文本显示在一行中,没有换行符。
I am using Epplus library to convert dataTable
in Excel. I am using a textarea in my front end site. In which a line break is also there. But, the problem is when I convert this file to Excel, text shows in one line not with a line break.
如何在Excel Epplus中添加换行符。
How can I add a line break in Excel Epplus.
我正在使用以下脚本:
using (ExcelPackage pck = new ExcelPackage(newFile)) {
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Accounts");
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
pck.Save();
}
推荐答案
您是否打开了文本包裹细胞?默认情况下它是关闭的。像这样:
Did you to turn on text wrap on the cells? It is off by default. So something like this:
<body>
<form id="form1" runat="server">
<div>
<textarea id="TextArea1" style="height: 200px; width: 400px;" runat="server"></textarea>
</div>
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_OnClick" Text="Button" />
</div>
</form>
</body>
然后:
protected void Button1_OnClick(object sender, EventArgs e)
{
//Create the table from the textbox
var dt = new DataTable();
dt.Columns.Add("Column1");
var dr = dt.NewRow();
dr[0] = TextArea1.InnerText;
dt.Rows.Add(dr);
var excelDocName = @"c:\temp\temp.xlsx";
var aFile = new FileInfo(excelDocName);
if (aFile.Exists)
aFile.Delete();
var pck = new ExcelPackage(aFile);
var ws = pck.Workbook.Worksheets.Add("Content");
ws.Cells["A1"].LoadFromDataTable(dt, true);
ws.Cells["A1:A2"].Style.WrapText = true; //false by default
pck.Save();
pck.Dispose();
}
我将其放在文本区域:
This is line 1.
This is line 3.
This is line 5.
哪个打开并显示换行符。
Which opens with line breaks shown.
这篇关于如何在Excel epplus C#中给换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文