C# 在C中使用OpenXml在Excel中创建日期单元格时遇到的问题#
我需要将数据表导出到excel文件。我是使用DocumentFormat.OpenXml完成的。但我无法在导出的excel中获取日期(年-月-日)过滤器,因为我的单元格被视为字符串,而不是excel日期单元格。我使用interop.dll获得它,但它很耗时。我有超过30000张唱片。感谢您的帮助。我的代码如下:-C# 在C中使用OpenXml在Excel中创建日期单元格时遇到的问题#,c#,excel,openxml,C#,Excel,Openxml,我需要将数据表导出到excel文件。我是使用DocumentFormat.OpenXml完成的。但我无法在导出的excel中获取日期(年-月-日)过滤器,因为我的单元格被视为字符串,而不是excel日期单元格。我使用interop.dll获得它,但它很耗时。我有超过30000张唱片。感谢您的帮助。我的代码如下:- protected void Page_Load(object sender, EventArgs e) { DataSet ds=new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds=new DataSet();
ds.Tables.Add(GetData());
GenerateExcel(ds);
}
public void GenerateExcel(DataSet data)
{
var Memstream = new MemoryStream();
using (var workbook = SpreadsheetDocument.Create(Memstream, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
WorkbookStylesPart wbsp = workbookPart.AddNewPart<WorkbookStylesPart>();
wbsp.Stylesheet = CreateStylesheet1();
wbsp.Stylesheet.Save();
uint sheetId = 0;
foreach (System.Data.DataTable table in data.Tables)
{
var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
int k = sheets.ChildElements.Count;
string bsd = sheets.InnerText;
string jknkj = sheets.InnerXml;
sheetId = sheetId++;
DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
sheets.Append(sheet);
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<String> columns = new List<string>();
foreach (DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (DataRow dsrow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach (String col in columns)
{
DateTime dDate;
if (DateTime.TryParse(dsrow[col].ToString(), out dDate))
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number;
cell.StyleIndex = 164;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dDate.ToOADate().ToString());
newRow.AppendChild(cell);
}
else
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString());
newRow.AppendChild(cell);
}
}
sheetData.AppendChild(newRow);
}
}
workbookPart.Workbook.Save();
workbook.Close();
}
string filename=@"D:\\Excel Samples\file_" + DateTime.Now.ToString("MM-dd-yyyyHH-mm-ss") + ".xlsx";
FileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write);
Memstream.WriteTo(file);
file.Close();
Memstream.Close();
}
private Stylesheet CreateStylesheet1()
{
Stylesheet ss = new Stylesheet();
Fonts fts = new Fonts();
DocumentFormat.OpenXml.Spreadsheet.Font ft = new DocumentFormat.OpenXml.Spreadsheet.Font();
FontName ftn = new FontName();
ftn.Val = "Calibri";
DocumentFormat.OpenXml.Spreadsheet.FontSize ftsz = new DocumentFormat.OpenXml.Spreadsheet.FontSize();
ftsz.Val = 11;
ft.FontName = ftn;
ft.FontSize = ftsz;
fts.Append(ft);
fts.Count = (uint)fts.ChildElements.Count;
Fills fills = new Fills();
Fill fill;
PatternFill patternFill;
fill = new Fill();
patternFill = new PatternFill();
patternFill.PatternType = PatternValues.None;
fill.PatternFill = patternFill;
fills.Append(fill);
fill = new Fill();
patternFill = new PatternFill();
patternFill.PatternType = PatternValues.Gray125;
fill.PatternFill = patternFill;
fills.Append(fill);
fills.Count = (uint)fills.ChildElements.Count;
Borders borders = new Borders();
Border border = new Border();
border.LeftBorder = new LeftBorder();
border.RightBorder = new RightBorder();
border.TopBorder = new TopBorder();
border.BottomBorder = new BottomBorder();
border.DiagonalBorder = new DiagonalBorder();
borders.Append(border);
borders.Count = (uint)borders.ChildElements.Count;
CellStyleFormats csfs = new CellStyleFormats();
CellFormat cf = new CellFormat();
cf.NumberFormatId = 0;
cf.FontId = 0;
cf.FillId = 0;
cf.BorderId = 0;
csfs.Append(cf);
csfs.Count = (uint)csfs.ChildElements.Count;
uint iExcelIndex = 164;
NumberingFormats nfs = new NumberingFormats();
CellFormats cfs = new CellFormats();
cf = new CellFormat();
cf.NumberFormatId = 0;
cf.FontId = 0;
cf.FillId = 0;
cf.BorderId = 0;
cf.FormatId = 0;
cfs.Append(cf);
NumberingFormat nf;
nf = new NumberingFormat();
nf.NumberFormatId = iExcelIndex++;
nf.FormatCode = StringValue.FromString("mm/dd/yyyy hh:mm:ss");
//nf.FormatCode = StringValue.FromString("mm-d-yy h:mm:ss AM/PM");
nfs.Append(nf);
cf = new CellFormat();
cf.ApplyNumberFormat = true;
cf.NumberFormatId = nf.NumberFormatId;
cf.FontId = 0;
cf.FillId = 0;
cf.BorderId = 0;
cf.FormatId = 0;
cfs.Append(cf);
nf = new NumberingFormat();
nf.NumberFormatId = iExcelIndex++;
nf.FormatCode = "#,##0.0000";
nfs.Append(nf);
cf = new CellFormat();
cf.NumberFormatId = nf.NumberFormatId;
cf.FontId = 0;
cf.FillId = 0;
cf.BorderId = 0;
cf.FormatId = 0;
cf.ApplyNumberFormat = true;
cfs.Append(cf);
// #,##0.00 is also Excel style index 4
nf = new NumberingFormat();
nf.NumberFormatId = iExcelIndex++;
nf.FormatCode = "#,##0.00";
nfs.Append(nf);
cf = new CellFormat();
cf.NumberFormatId = nf.NumberFormatId;
cf.FontId = 0;
cf.FillId = 0;
cf.BorderId = 0;
cf.FormatId = 0;
cf.ApplyNumberFormat = true;
cfs.Append(cf);
// @ is also Excel style index 49
nf = new NumberingFormat();
nf.NumberFormatId = iExcelIndex++;
nf.FormatCode = "@";
nfs.Append(nf);
cf = new CellFormat();
cf.NumberFormatId = nf.NumberFormatId;
cf.FontId = 0;
cf.FillId = 0;
cf.BorderId = 0;
cf.FormatId = 0;
cf.ApplyNumberFormat = true;
cfs.Append(cf);
nfs.Count = (uint)nfs.ChildElements.Count;
cfs.Count = (uint)cfs.ChildElements.Count;
ss.Append(nfs);
ss.Append(fts);
ss.Append(fills);
ss.Append(borders);
ss.Append(csfs);
ss.Append(cfs);
CellStyles css = new CellStyles();
CellStyle cs = new CellStyle();
cs.Name = "Normal";
cs.FormatId = 0;
cs.BuiltinId = 0;
css.Append(cs);
css.Count = (uint)css.ChildElements.Count;
ss.Append(css);
DifferentialFormats dfs = new DifferentialFormats();
dfs.Count = 0;
ss.Append(dfs);
TableStyles tss = new TableStyles();
tss.Count = 0;
tss.DefaultTableStyle = "TableStyleMedium9";
tss.DefaultPivotStyle = "PivotStyleLight16";
ss.Append(tss);
return ss;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
数据集ds=新数据集();
Add(GetData());
GenerateExcel(ds);
}
public void GenerateExcel(数据集数据)
{
var Memstream=new MemoryStream();
使用(var工作簿=SpreadsheetDocument.Create(Memstream,DocumentFormat.OpenXml.SpreadsheetDocumentType.工作簿))
{
var workbookPart=workbook.AddWorkbookPart();
workbook.WorkbookPart.workbook=newdocumentformat.OpenXml.Spreadsheet.workbook();
workbook.WorkbookPart.workbook.Sheets=新文档格式.OpenXml.Spreadsheet.Sheets();
WorkbookStylesPart wbsp=workbookPart.AddNewPart();
wbsp.Stylesheet=CreateStylesheet1();
wbsp.Stylesheet.Save();
uint sheetId=0;
foreach(Data.Tables中的System.Data.DataTable表)
{
var sheetPart=workbook.WorkbookPart.AddNewPart();
var sheetData=new DocumentFormat.OpenXml.Spreadsheet.sheetData();
sheetPart.Worksheet=newdocumentformat.OpenXml.Spreadsheet.Worksheet(sheetData);
DocumentFormat.OpenXml.Spreadsheet.Sheets=工作簿.WorkbookPart.workbook.GetFirstChild();
string relationshipId=工作簿.WorkbookPart.GetIdOfPart(sheetPart);
int k=sheets.ChildElements.Count;
字符串bsd=sheets.InnerText;
字符串jknkj=sheets.InnerXml;
sheetId=sheetId++;
DocumentFormat.OpenXml.Spreadsheet.Sheet Sheet=新的DocumentFormat.OpenXml.Spreadsheet.Sheet(){Id=relationshipId,SheetId=SheetId,Name=table.TableName};
附页(页);
DocumentFormat.OpenXml.Spreadsheet.Row headerRow=新的DocumentFormat.OpenXml.Spreadsheet.Row();
列表列=新列表();
foreach(table.Columns中的DataColumn列)
{
columns.Add(column.ColumnName);
DocumentFormat.OpenXml.Spreadsheet.Cell=新DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType=DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue=newdocumentformat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
头附子(单元);
}
sheetData.AppendChild(headerRow);
foreach(table.Rows中的数据行dsrow)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow=新DocumentFormat.OpenXml.Spreadsheet.Row();
foreach(列中的字符串列)
{
日期时间数据;
if(DateTime.TryParse(dsrow[col].ToString(),out-dDate))
{
DocumentFormat.OpenXml.Spreadsheet.Cell=新DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType=DocumentFormat.OpenXml.Spreadsheet.CellValues.Number;
cell.StyleIndex=164;
cell.CellValue=newdocumentformat.OpenXml.Spreadsheet.CellValue(dDate.ToOADate().ToString());
newRow.AppendChild(单元格);
}
其他的
{
DocumentFormat.OpenXml.Spreadsheet.Cell=新DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType=DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue=newdocumentformat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString());
newRow.AppendChild(单元格);
}
}
sheetData.AppendChild(纽罗);
}
}
workbookPart.Workbook.Save();
workbook.Close();
}
字符串文件名=@“D:\\Excel Samples\file”+DateTime.Now.ToString(“MM-dd-yyhh-MM-ss”)+“.xlsx”;
FileStream file=newfilestream(文件名,FileMode.Create,FileAccess.Write);
Memstream.WriteTo(文件);
file.Close();
Memstream.Close();
}
私有样式表CreateStylesheet1()
{
样式表ss=新样式表();
字体fts=新字体();
DocumentFormat.OpenXml.Spreadsheet.Font ft=新建DocumentFormat.OpenXml.Spreadsheet.Font();
FontName ftn=新FontName();
ftn.Val=“Calibri”;
DocumentFormat.OpenXml.Spreadsheet.FontSize ftsz=新的DocumentFormat.OpenXml.Spreadsheet.FontSize();
ftsz.Val=11;
ft.FontName=ftn;
ft.FontSize=ftsz;
附加(ft);
fts.Count=(uint)fts.ChildElements.Count;
填充=新填充();
填充;
填充图案填充图案填充;
填充=新填充();
patternFill=新的patternFill();
patternFill.PatternType=PatternValues.None;
fill.PatternFill=PatternFill;
填充。追加(填充);
填充=新填充();
patternFill=新的patternFill();
patternFill.PatternType=PatternValues.Gray125;
fill.PatternFill=PatternFill;
填充。追加(填充);
fills.Count=(uint)fills.ChildElements.Count;
边界=新边界();
边框=新边框();
border.LeftBorder=新的LeftBorder();
border.RightBorder=新的RightBorder();
博德