C# Open XML SDK 在特定单元格引用处写入数据
C# Open XML SDK write data at specific cell reference
我有一个 excel sheet 的单元格,其中的单元格引用已更改。
使用我的代码,我可以加载 excel sheet 并将我的数据加载到其中。但我希望我的数据将开始插入特定单元格 "int_startdok"。
我的代码:
private byte[] LoadDataToFile([NotNull] DataTable dataTable)
{
if (dataTable == null) throw new ArgumentNullException(nameof(dataTable));
var memoryStream = new MemoryStream();
var byteArray = File.ReadAllBytes(TemplateFilePath);
memoryStream.Write(byteArray, 0, byteArray.Length);
using (var spreadsheetDocument = SpreadsheetDocument.Open(memoryStream, true))
{
var workbookPart = spreadsheetDocument.WorkbookPart;
var worksheetPart = workbookPart.WorksheetParts.First();
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
var headerRow = new Row();
var columns = new List<string>();
foreach (DataColumn column in dataTable.Columns)
{
columns.Add(column.ColumnName);
var cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
foreach (DataRow dsrow in dataTable.Rows)
{
var newRow = new Row();
foreach (var col in columns)
{
var cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(dsrow[col].ToString());
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
}
return memoryStream.ToArray();
}
我该怎么做?
我找到了解决问题的方法。与 ClosedXML i could do the trick very easy and reduce my code. For ClosedXML you need Open XML SDK。您可以从 ClosedXML Wiki 页面获取它。
这是我的解决方案:
private byte[] InsertDataToTemplate([NotNull] DataTable dataTable)
{
if (dataTable == null) throw new ArgumentNullException(nameof(dataTable));
// Create a memorystream from the template
var memoryStream = new MemoryStream();
var byteArray = File.ReadAllBytes(TemplateFilePath);
memoryStream.Write(byteArray, 0, byteArray.Length);
using (var workbook = new XLWorkbook(memoryStream))
{
var sheetData = workbook.Worksheets.First();
sheetData.Cell("int_startdok").InsertData(dataTable.Rows);
workbook.Save();
}
return memoryStream.ToArray();
}
我有一个 excel sheet 的单元格,其中的单元格引用已更改。
使用我的代码,我可以加载 excel sheet 并将我的数据加载到其中。但我希望我的数据将开始插入特定单元格 "int_startdok"。
我的代码:
private byte[] LoadDataToFile([NotNull] DataTable dataTable)
{
if (dataTable == null) throw new ArgumentNullException(nameof(dataTable));
var memoryStream = new MemoryStream();
var byteArray = File.ReadAllBytes(TemplateFilePath);
memoryStream.Write(byteArray, 0, byteArray.Length);
using (var spreadsheetDocument = SpreadsheetDocument.Open(memoryStream, true))
{
var workbookPart = spreadsheetDocument.WorkbookPart;
var worksheetPart = workbookPart.WorksheetParts.First();
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
var headerRow = new Row();
var columns = new List<string>();
foreach (DataColumn column in dataTable.Columns)
{
columns.Add(column.ColumnName);
var cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
foreach (DataRow dsrow in dataTable.Rows)
{
var newRow = new Row();
foreach (var col in columns)
{
var cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(dsrow[col].ToString());
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
}
return memoryStream.ToArray();
}
我该怎么做?
我找到了解决问题的方法。与 ClosedXML i could do the trick very easy and reduce my code. For ClosedXML you need Open XML SDK。您可以从 ClosedXML Wiki 页面获取它。
这是我的解决方案:
private byte[] InsertDataToTemplate([NotNull] DataTable dataTable)
{
if (dataTable == null) throw new ArgumentNullException(nameof(dataTable));
// Create a memorystream from the template
var memoryStream = new MemoryStream();
var byteArray = File.ReadAllBytes(TemplateFilePath);
memoryStream.Write(byteArray, 0, byteArray.Length);
using (var workbook = new XLWorkbook(memoryStream))
{
var sheetData = workbook.Worksheets.First();
sheetData.Cell("int_startdok").InsertData(dataTable.Rows);
workbook.Save();
}
return memoryStream.ToArray();
}