【发布时间】:2014-01-29 08:59:11
【问题描述】:
请参考我用来从数据表生成 excel 文件的代码链接。我能够成功生成excel文件。
但是问题/挑战/问题如下。
- 我想根据数据类型生成列,所以如果列值包含日期,那么它的单元格格式应该是日期(dd/mm/yyy),如果数字然后是数字。等等...
- 我已经尝试根据数据格式生成excel文件,您可以查看生成单元格值的具体方法。但问题是当用户下载文件时,它会给出警告消息“Excel发现不可读的内容'文件名'。你想恢复这个工作簿的内容吗?”。我不希望出现该警告消息。
- 如果我将所有内容都写为没有格式的文本,则文件将打开而没有任何警告消息,并且在下载文件后,如果用户尝试以日期或数字格式格式化相应的列,那么它也不允许用户格式化/切片和切块excel 文件中的数据。
参考:- http://www.codeproject.com/Articles/263106/Export-Tabular-Data-in-CSV-and-Excel-Formats-Throu
如果有人知道,请告诉我解决方案。
我正在使用DocumentFormat.OpenXml.dll
private Cell CreateTextCell(string header, UInt32 index, string text)
{
var cell = new Cell {DataType = CellValues.InlineString, CellReference = header + index};
var istring = new InlineString();
var t = new Text {Text = text};
istring.Append(t);
cell.Append(istring);
return cell;
}
private Cell CreateDateCell(string header, UInt32 index, DateTime sDate)
{
Cell cell = new Cell();
cell.DataType = CellValues.Date;
cell.CellReference = header + index;
cell.StyleIndex = UInt32Value.FromUInt32(14);
cell.CellValue = new CellValue { Text = sDate.ToOADate().ToString() };
return cell;
}
private Cell CreateNumberCell(string header, UInt32 index, string text)
{
Cell cell = new Cell();
cell.DataType = CellValues.Number;
cell.CellReference = header + index;
cell.CellValue = new CellValue(text);
return cell;
}
【问题讨论】:
-
作为建议:使用 EPPLUS 之类的东西。它很好地隐藏了 Open XMl SDK 的低级管道。我和两者都合作过,不能说我是多么喜欢 EPPLUS ;)
-
是的,即使是许可证也对开发人员非常有利。
-
如果有人知道使用 Open XML SDK 的解决方案,那么它将帮助我很多...
标签: c# excel openxml-sdk