简体   繁体   English

使用ExcelLibrary c#使用列日期时间从SP生成Excel文件的问题

[英]Issue generating an excel file from a SP with a column datetime using ExcelLibrary c#

I'm using the ExcelLibrary to export the result from a SP to Excel following Mike Webb's example at: Create Excel (.XLS and .XLSX) file from C# 我正在使用ExcelLibrary将结果从SP导出到Excel,遵循Mike Webb的示例: 从C#创建Excel(.XLS和.XLSX)文件

But one of the columns of the SP returns a DateTime field, and every time I generate the excel File that column appears with a different value. 但是,SP的其中一列返回DateTime字段,每次生成excel文件时,该列都会显示不同的值。

Example: 例:

In SP: 08/05/2013  5:27PM
In Excel: 40029.72778

Is there any way to fix this? 有没有什么办法解决这一问题? I believe that this is because of the cell format. 我相信这是因为细胞格式。

You don't show any code but I believe the problem is you need to specify formatting for the ExcelLibrary.SpreadSheet.Cell object. 您没有显示任何代码,但我认为问题是您需要为ExcelLibrary.SpreadSheet.Cell对象指定格式。

The following code demonstrates the problem you're seeing and formats the Cell to correct it: 以下代码演示了您所看到的问题并格式化Cell以更正它:

// DateTime = 08/05/2013  5:27PM, from your SP.
var dateTime = new DateTime(2013, 8, 5, 17, 27, 00);

string file = "..\\..\\..\\..\\newdoc1.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");

// This reproduces your problem.
worksheet.Cells[0, 0] = new Cell(dateTime);
// This corrects the problem by specifying Cell formatting.
worksheet.Cells[0, 1] = new Cell(dateTime, @"YYYY\-MM\-DD");

workbook.Worksheets.Add(worksheet);

workbook.Save(file);

Documentation for this project is pretty light, but various Cell formatting options are demonstrated on the project's Google Code home page . 该项目的文档非常简单,但在项目的Google Code主页上演示了各种Cell格式选项。

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

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