我想创建一个具有多个自动生成的表格的新Excel。 这是我创建的模板方法。 我从ExportNormTableController.cs中调用了Generate方法。 我想修复该文件损坏并直接打开excel,因为在生成文件后,我要使用excel并打开它,我应该创建一个新工作表来读取多个.fdf文件并将内容放入新创建的工作表。
$ jq -r .ans1 <<< "$json"
first line
second line
third line
我认为问题出在SheetData的创建上,但是我不确定。 请帮助我任何人:) 我还从以下位置打开了文件:右键单击文件> 7Zip> openArchive 并比较了文件(已修复的文件和初始的文件),这里缺少一个文件夹:DocProps
进行模板调用的方法如下:
namespace ExportNormtables
{
public class ExportNormTablesTemplate : IDisposable
{
SpreadsheetDocument document;
private MemoryStream stream = new MemoryStream();
public ExportNormTablesTemplate(string filepath)
{
document = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
}
public byte[] Generate(DataSet ds)
{
using (document)
{
if (ds != null)
{
// Add a WorkbookPart to the document.
WorkbookPart workBookPart = document.AddWorkbookPart();
workBookPart.Workbook = new Workbook();
Sheets sheets = document.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
foreach (System.Data.DataTable table in ds.Tables)
{
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart sheetPart = workBookPart.AddNewPart<WorksheetPart>();
sheetPart.Worksheet = new Worksheet(new SheetData());
uint sheetId = 1;
if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
{
sheetId = sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
Sheet sheet = new Sheet() { Id = document.WorkbookPart.GetIdOfPart(sheetPart), SheetId = sheetId, Name = table.TableName };
sheets.Append(sheet);
// Get the sheetData cell table.
SheetData sheetData = sheetPart.Worksheet.GetFirstChild<SheetData>();
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<String> columns = new List<string>();
foreach (System.Data.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 (System.Data.DataRow dsrow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach (String col in columns)
{
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);
}
}
document.Save();
}
// This is a workaround because of
// https://github.com/OfficeDev/Open-XML-SDK/issues/294
var newStream = new MemoryStream();
document.Clone(newStream);
return newStream.ToArray();
}
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
if (document != null)
{
document.Dispose();
document = null;
}
if (stream != null)
{
stream.Dispose();
stream = null;
}
}
disposedValue = true;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1816:CallGCSuppressFinalizeCorrectly")]
public void Dispose()
{
Dispose(true);
}
#endregion
}
}