gpt4 book ai didi

java - 修复记录: Cell information from worksheet created

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:57 26 4
gpt4 key购买 nike

我在打开 OpenXML 创建的电子表格时收到错误。错误如下。

repaired record : xl/worksheets/sheet.xml partial cell information

private void SavexlsExcelFile(String fullPathName)
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(fullPathName, SpreadsheetDocumentType.Workbook))
{

WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();

worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();

Columns columns = new Columns();

worksheetPart.Worksheet.AppendChild(columns);

Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet" };
sheets.Append(sheet);

workbookPart.Workbook.Save();

sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());


List<List<string>> dataRow = new List<List<string>>();
List<String> dtRow = new List<String>();

Row row = new Row();

for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
if (i == 0)
{
Cell dataCell = new Cell();
dataCell.DataType = CellValues.String;
CellValue cellValue = new CellValue();

cellValue.Text = dataGridView1.Columns[j].Name;
dataCell.StyleIndex = 2;
dataCell.Append(cellValue);
row.AppendChild(dataCell);
//dataColumn.Add(dataGridView1.Columns[j].Name);
}
dtRow.Add(dataGridView1.Rows[i].Cells[j].Value.ToString());
}
}

dataRow.Add(dtRow);
sheetData.AppendChild(row);

row = new Row();

foreach (List<string> datarow in dataRow)
{
row = new Row();
foreach(string dtrow in datarow)
{
row.Append(ConstructCell(dtrow, CellValues.String, 2));
}
sheetData.AppendChild(row);
}
worksheetPart.Worksheet.Save();
}
}
private Cell ConstructCell(string value, CellValues dataType, uint styleIndex = 0)
{
return new Cell()
{
CellValue = new CellValue(value),
DataType = new EnumValue<CellValues>(dataType),
StyleIndex = styleIndex
};
}

最佳答案

我可以看到这里有两个问题。首先是您对Columns的使用不正确。如果您希望控制列宽等内容,则应该使用Columns。要正确使用 Columns,您需要添加子 Column 元素。例如(取自 here ):

Columns columns = new Columns();

columns.Append(new Column() { Min = 1, Max = 3, Width = 20, CustomWidth = true });
columns.Append(new Column() { Min = 4, Max = 4, Width = 30, CustomWidth = true });

在您的示例中,您可以删除以下两行

Columns columns = new Columns();
worksheetPart.Worksheet.AppendChild(columns);

第二个问题是您正在使用的StyleIndex;您的文档中不存在该样式,因为您尚未添加它。这里最简单的事情就是完全删除 StyleIndex

当调试这样的文件时,总是值得查看 OpenXml Productivity Tool 。您可以在该工具中打开生成的文件并验证它以查看文件中存在哪些错误。

关于java - 修复记录: Cell information from worksheet created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com