OpenXML库保存Excel文件 [英] OpenXML library save excel file
本文介绍了OpenXML库保存Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,它没有将值保存到单元格以及文件中.它在cell.cellvalue
字段中显示值,但未将其写入excel.我不知道如何保存文件.我使用了 OpenXml-SDK ,并且正在将datatable
值写入创建的电子表格文档的每个cell/row
中.
I have the following code and its not saving the values to cell and also in file. It shows value in cell.cellvalue
field but its not writing it to excel. I have no idea how to save the file. I used OpenXml-SDK and I am writing datatable
values to each cell/row
of created spreadsheet document.
using (SpreadsheetDocument ssd=SpreadsheetDocument.Open(Server.MapPath(@"\ExcelPackageTemplate.xlsx"),true))
{
WorkbookPart wbPart = ssd.WorkbookPart;
WorksheetPart worksheetPart = wbPart.WorksheetParts.First();
SheetData sheetdata = worksheetPart.Worksheet.GetFirstChild<SheetData>();
string[] headerColumns = new string[] { dt.Columns[0].ColumnName, dt.Columns[1].ColumnName,dt.Columns[2].ColumnName };
DocumentFormat.OpenXml.Spreadsheet.Row r = new DocumentFormat.OpenXml.Spreadsheet.Row();
int RowIndexer = 1;
//int colInd=0;
r.RowIndex = (UInt32)RowIndexer;
string test = ColumnName(RowIndexer);
foreach (DataColumn dc in dt.Columns)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.CellReference = test+RowIndexer;
cell.DataType = CellValues.InlineString;
cell.InlineString = new InlineString(new Text(dc.ColumnName.ToString()));
DocumentFormat.OpenXml.Spreadsheet.CellValue value = new DocumentFormat.OpenXml.Spreadsheet.CellValue();
r.AppendChild(cell);
// colInd++;
}
//r.RowIndex = (UInt32)RowIndexer;
RowIndexer = 2;
foreach (DataRow dr in dt.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row Row = new DocumentFormat.OpenXml.Spreadsheet.Row();
string Index = ColumnName(RowIndexer);
Row.RowIndex = (UInt32)RowIndexer;
foreach (object value in dr.ItemArray)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = CellValues.InlineString;
cell.InlineString = new InlineString(new Text(value.ToString()));
cell.CellReference = Index+RowIndexer;
// cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value.ToString());
Row.AppendChild(cell);
}
RowIndexer++;
}
worksheetPart.Worksheet.Save();
wbPart.Workbook.Save();
ssd.Close();
推荐答案
尝试一下:
using (SpreadsheetDocument ssd = SpreadsheetDocument.Open(Server.MapPath(@"\ExcelPackageTemplate.xlsx"), true))
{
WorkbookPart wbPart = ssd.WorkbookPart;
WorksheetPart worksheetPart = wbPart.WorksheetParts.First();
SheetData sheetdata = worksheetPart.Worksheet.GetFirstChild<SheetData>();
string[] headerColumns = new string[] { dt.Columns[0].ColumnName, dt.Columns[1].ColumnName, dt.Columns[2].ColumnName };
DocumentFormat.OpenXml.Spreadsheet.Row r = new DocumentFormat.OpenXml.Spreadsheet.Row();
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
int RowIndexer = 1;
int ColumnIndexer = 1;
r.RowIndex = (UInt32)RowIndexer;
foreach (DataColumn dc in dt.Columns)
{
cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.CellReference = ColumnName(ColumnIndexer) + RowIndexer;
cell.DataType = CellValues.InlineString;
cell.InlineString = new InlineString(new Text(dc.ColumnName.ToString()));
// consider using cell.CellValue. Then you don't need to use InlineString.
// Because it seems you're not using any rich text so you're just bloating up
// the XML.
r.AppendChild(cell);
ColumnIndexer++;
}
// here's the missing part you needed
sheetdata.Append(r);
RowIndexer = 2;
foreach (DataRow dr in dt.Rows)
{
r = new DocumentFormat.OpenXml.Spreadsheet.Row();
r.RowIndex = (UInt32)RowIndexer;
// this follows the same starting column index as your column header.
// I'm assuming you start with column 1. Change as you see fit.
ColumnIndexer = 1;
foreach (object value in dr.ItemArray)
{
cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
// I moved it here so it's consistent with the above part
// Also, the original code was using the row index to calculate
// the column name, which is weird.
cell.CellReference = ColumnName(ColumnIndexer) + RowIndexer;
cell.DataType = CellValues.InlineString;
cell.InlineString = new InlineString(new Text(value.ToString()));
r.AppendChild(cell);
ColumnIndexer++;
}
RowIndexer++;
// missing part
sheetdata.Append(r);
}
worksheetPart.Worksheet.Save();
wbPart.Workbook.Save();
ssd.Close();
}
一些评论:
- ColumnName()函数来自此处.
- 我假设您想要一行中的列标题以及后续行中的数据(因为原始代码具有用于计算列名的行索引).
- 清理了部分代码,使其更易于阅读,并且列标题和数据行部分的书写样式更加一致.
- 我建议您考虑使用CellValue而不是InlineString.根据您的代码,您正在导入一个DataTable,并且您似乎不需要富文本格式,因此InlineString有点过大,可能会使生成的文件更大(XML膨胀).使用一个或另一个,并记住相应地设置DataType.
- 此外,该代码仅适用于完全空的SheetData.
- The ColumnName() function is from here.
- I'm assuming you wanted the column headers in a row, and data in subsequent rows (because the original code had the row index used for calculating the column name).
- Cleaned up some parts of the code so it's easier to read and that the column header and data row parts are more consistent in writing style.
- I suggest that you consider using CellValue instead of InlineString. Based on your code, you're importing a DataTable, and you don't seem to need rich text so InlineString's are a little overkill and might make the resulting file larger (bloated XML). Use one or the other, and remember to set DataType accordingly.
- Also, the code only works for completely empty SheetData.
这篇关于OpenXML库保存Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文