OpenXML SDK 2.5 不可读的内容
OpenXML SDK 2.5 unreadable content
我正在处理现有的 Excel 文件,该文件已经包含所有公式和格式,我将数据添加到 sheet 和 Table 然后当我在 Excel 中打开该文件我收到错误
"Excel completed file level validation and repair. Some parts of this
workbook may have been repaired or discarded. Removed Records: Cell
information from /xl/worksheets/sheet6.xml part"
然后我打开了一个内容相同的手动创建的文件,它工作得很好。
我还发现了 Open XML 2.5 Productivity Tool,当我对生成的文件进行 运行 验证时,它说没有发现问题。
当我 运行 比较两个文件时,我看到生成的文件如下所示。
<x:c r="B462" t="inlineStr">
<x:is>
<x:t>1150828</x:t>
</x:is>
</x:c>
虽然手动创建的文件具有如下所示的单元格。
<c s="80" r="B462">
<v>
1150828
</v>
</c>
显然这里存在差异,但我不知道如何更正它,也不知道这种差异是否是错误的实际原因。但看到其他一切看起来都一样,我不知道它还能是什么。
哦,还有一些事情这个文件不起作用,但我可以使用另一个不包含 Table 的文件,当我合并 table 时,问题就出现了,所以我至少知道那么多。
此外,如果您要建议我使用 ClosedXML,请不要这样做。我已经使用过它,但由于某种我无法弄清楚的原因,它往往会随机停止格式化,因此我为什么要转向 OpenXML SDk
这是一些 C# 代码
dt.Load(reader);
RowCount = dt.Rows.Count;
ColumnCount = dt.Columns.Count;
workbookPart = spreadDoc.WorkbookPart;
SheetDimension sheetDimension = new SheetDimension() { Reference = "A1:" + ColumnLetters[ColumnCount - 1] + (RowCount + 1) };
worksheetPart = Program.GetWorksheetPart(workbookPart, reportStep.ExcelSheetName);
worksheetPart.Worksheet.SheetDimension = sheetDimension;
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
string relId = workbookPart.Workbook.Descendants<Sheet>().First(s => reportStep.ExcelSheetName.Equals(s.Name)).Id;
if (reportStep.ExcelTableExists)
{
TableDefinitionPart tableDef = null;
int looper = 0;
foreach (WorksheetPart wsp in spreadDoc.WorkbookPart.WorksheetParts)
{
if (wsp.TableDefinitionParts.Where(tbl => tbl.Table.DisplayName.Value.Equals(reportStep.ExcelTableName)).Count() == 1)
{
tableDef = spreadDoc.WorkbookPart.WorksheetParts.ElementAt(looper).TableDefinitionParts.Where(tbl => tbl.Table.DisplayName.Value.Equals(reportStep.ExcelTableName)).FirstOrDefault();
tableDef.Table.Reference.Value = "A1:" + (ColumnLetters[ColumnCount - 1] + (RowCount +1) ).ToString();
tableDef.Table.AutoFilter.Reference.Value = "A1:" + (ColumnLetters[ColumnCount - 1] + (RowCount +1)).ToString();
// tabledefinitionPart = Program.GetTablePart(wsp, reportStep.ExcelTableName, ColumnCount, RowCount);
}
looper++;
}
}
sheetData = Chef.Program.ExportDataTable(dt, sheetData);
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == reportStep.ExcelSheetName);
public static TableDefinitionPart GetTablePart(WorksheetPart worksheet, string tablename, int columnCount, int rowCount)
{
uint CellRange = (uint)(columnCount);
TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)(CellRange) };
var tableDefPart = worksheet.TableDefinitionParts.Where(tbl => tbl.Table.DisplayName.Value.Equals(tablename)).FirstOrDefault();
//worksheet.WorksheetPart.TableDefinitionParts.AddNewPart<TableDefinitionPart>(tablename);
var table = new Table() { HeaderRowCount = (uint)columnCount, Name = tablename, DisplayName = tablename, Reference = "A1:" + ColumnLetters[columnCount -1] + (rowCount + 1), TotalsRowShown = false };
TableStyleInfo tableStyleInfo1 = new TableStyleInfo()
{
Name = "TableStyleMedium2",
ShowFirstColumn = false,
ShowLastColumn = false,
ShowRowStripes = true,
ShowColumnStripes = false
};
table.Append(tableStyleInfo1);
// table.Append(tableColumns1);
tableDefPart.Table = table;
return tableDefPart;
}
编辑部分添加请求的其他方法更新于 2015 年 9 月 5 日
我确实删除了添加 header 值的代码,因为它们已经是 excel 文件基本模板的一部分。还删除了单元格数据类型的指定以保留模板已经将单元格数据类型设置为的内容。
public static SheetData ExportDataTable2(System.Data.DataTable exportData, SheetData sheetData)
{
//loop through each data row
DataRow contentRow;
int startRow = 2;
for (int i = 0; i < exportData.Rows.Count; i++)
{
contentRow = exportData.Rows[i];
sheetData.AppendChild(createContentRow(contentRow, i + startRow));
}
return sheetData;
}
private static Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
// cell.DataType = CellValues.Number;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
cell.CellValue = new CellValue(cellValue.ToString());
return cell;
}
private static Row createContentRow(DataRow dataRow, int rowIndex)
{
Row row = new Row
{
RowIndex = (UInt32)rowIndex
};
for (int i = 0; i < dataRow.Table.Columns.Count; i++)
{
Cell dataCell = createTextCell(i + 1, rowIndex, dataRow[i]);
// dataCell.DataType = CellValues.SharedString;
row.AppendChild(dataCell);
}
return row;
}
我还遇到了有关使用 OpenXml Sdk 的无效文件的问题。
查看 OpenXml Power Tools; they've solved all my issues :) Also you should switch to the OpenXml Sdk 2.6 以避免 System.IO.Packaging
出现问题。希望对您有所帮助!
好吧,看来您已使用以下示例 OpenXML SDK 2.0: Export a DataTable to Excel 作为代码的基础。这是创建单元格的原始代码:
private Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
cell.DataType = CellValues.InlineString;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
InlineString inlineString = new InlineString();
Text t = new Text();
t.Text = cellValue.ToString();
inlineString.AppendChild(t);
cell.AppendChild(inlineString);
return cell;
}
您的原始代码除了以下行外完全相同:
cell.DataType = CellValues.String;
看出区别了吗?
那你改成:
private static Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
// cell.DataType = CellValues.Number;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
cell.CellValue = new CellValue(cellValue.ToString());
return cell;
}
好的,问题是你没有正确设置cell.DataType
。它需要与单元格内容同步,否则你会从 Excel 得到这样的错误。在前一种情况下,您将内容设置为 inline string
,但数据类型设置为 String
。在后面 - 数据类型为 Number
(您是否注释了该行并不重要 - Number
是单元格的默认数据类型)但内容并不总是数字(相同的功能用于列标题 - 毕竟,它称为 createTextCell)。
要解决此问题,请使用示例中的原始代码,或此代码:
private static Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
cell.CellValue = new CellValue(cellValue.ToString());
return cell;
}
最后,如果您需要存储共享字符串、数字、日期等,请阅读文档并设置适当的属性。我同意 OpenXml API 不是很直观,但这就是我们所拥有的。
编辑: 根据您的评论,您的真正问题似乎并不完全是问题所在。以下是使用不同数据类型列导出 DataTable
的高性能示例:
public static class ExcelExporter
{
public static void ExportDataTable(DataTable table, SheetData data)
{
var cellFactory = new CellFactory[table.Columns.Count];
for (int i = 0; i < table.Columns.Count; i++)
cellFactory[i] = GetCellFactory(table.Columns[i].DataType);
int rowIndex = 0;
data.AppendChild(CreateHeaderRow(rowIndex++, table));
for (int i = 0; i < table.Rows.Count; i++)
data.AppendChild(CreateContentRow(rowIndex++, table.Rows[i], cellFactory));
}
private static Row CreateHeaderRow(int rowIndex, DataTable table)
{
var row = CreateRow(rowIndex);
for (int i = 0; i < table.Columns.Count; i++)
{
var cell = CreateTextCell(i, rowIndex, table.Columns[i].ColumnName);
row.AppendChild(cell);
}
return row;
}
private static Row CreateContentRow(int rowIndex, DataRow dataRow, CellFactory[] cellFactory)
{
var row = CreateRow(rowIndex);
for (int i = 0; i < dataRow.Table.Columns.Count; i++)
{
var cell = cellFactory[i](i, rowIndex, dataRow[i]);
row.AppendChild(cell);
}
return row;
}
private static Row CreateRow(int index) { return new Row { RowIndex = (uint)index + 1 }; }
private delegate Cell CellFactory(int columnIndex, int rowIndex, object cellValue);
private static CellFactory GetCellFactory(Type dataType)
{
CellFactory factory;
return CellFactoryMap.TryGetValue(dataType, out factory) ? factory : TextCellFactory;
}
private static readonly CellFactory TextCellFactory = CreateTextCell;
private static readonly CellFactory DateCellFactory = CreateDateCell;
private static readonly CellFactory NumericCellFactory = CreateNumericCell;
private static readonly CellFactory BooleanCellFactory = CreateBooleanCell;
private static readonly Dictionary<Type, CellFactory> CellFactoryMap = new Dictionary<Type, CellFactory>
{
{ typeof(bool), BooleanCellFactory },
{ typeof(DateTime), DateCellFactory },
{ typeof(byte), NumericCellFactory },
{ typeof(sbyte), NumericCellFactory },
{ typeof(short), NumericCellFactory },
{ typeof(ushort), NumericCellFactory },
{ typeof(int), NumericCellFactory },
{ typeof(uint), NumericCellFactory },
{ typeof(long), NumericCellFactory },
{ typeof(ulong), NumericCellFactory },
{ typeof(float), NumericCellFactory },
{ typeof(double), NumericCellFactory },
{ typeof(decimal), NumericCellFactory },
};
private static Cell CreateTextCell(int columnIndex, int rowIndex, object cellValue)
{
return CreateCell(CellValues.String, columnIndex, rowIndex, ToExcelValue(cellValue));
}
private static Cell CreateDateCell(int columnIndex, int rowIndex, object cellValue)
{
// NOTE: CellValues.Date is not supported in older Excel version.
// In all Excel versions dates can be stored with CellValues.Number and a format style.
// Since I have no styles, will export them just as text
//var cell = CreateCell(CellValues.Number, columnIndex, rowIndex, ToExcelDate(cellValue));
//cell.StyleIndex = ...;
//return cell;
return CreateCell(CellValues.String, columnIndex, rowIndex,
cellValue != null && cellValue != DBNull.Value ? ((DateTime)cellValue).ToShortDateString() : null);
}
private static Cell CreateNumericCell(int columnIndex, int rowIndex, object cellValue)
{
return CreateCell(CellValues.Number, columnIndex, rowIndex, ToExcelValue(cellValue));
}
private static Cell CreateBooleanCell(int columnIndex, int rowIndex, object cellValue)
{
// NOTE: CellValues.Boolean is not supported in older Excel version
//return CreateCell(CellValues.Boolean, columnIndex, rowIndex, ToExcelValue(cellValue));
return CreateCell(CellValues.String, columnIndex, rowIndex, ToExcelValue(cellValue));
}
private static Cell CreateCell(CellValues dataType, int columnIndex, int rowIndex, string cellValue)
{
var cell = new Cell();
if (dataType != CellValues.Number) cell.DataType = dataType;
cell.CellReference = GetColumnName(columnIndex) + (rowIndex + 1);
cell.CellValue = new CellValue(cellValue ?? string.Empty);
return cell;
}
private static string ToExcelValue(object value)
{
if (value == null || value == DBNull.Value) return null;
return Convert.ToString(value, CultureInfo.InvariantCulture);
}
private static DateTime ExcelBaseDate = new DateTime(1900, 1, 1);
private static string ToExcelDate(object value)
{
const int days29Feb1900 = 59;
if (value == null || value == DBNull.Value) return null;
var date = ((DateTime)value).Date;
var days = (date - ExcelBaseDate).Days + 1;
if (days >= days29Feb1900) days++;
return days.ToString(CultureInfo.InvariantCulture);
}
private static string GetColumnName(int index) { return ColumnNameTable[index]; }
private static readonly string[] ColumnNameTable = BuildColumnNameTable();
private static string[] BuildColumnNameTable()
{
var table = new string[16384];
var sb = new StringBuilder();
for (int i = 0; i < table.Length; i++)
table[i] = sb.BuildColumnName(i);
return table;
}
private static string BuildColumnName(this StringBuilder sb, int index)
{
const int startLetter = 'A';
const int letterCount = 'Z' - startLetter + 1;
sb.Clear();
while (true)
{
var letter = (char)(startLetter + (index % letterCount));
sb.Insert(0, letter);
if (index < letterCount) break;
index = (index / letterCount) - 1;
}
return sb.ToString();
}
}
关键是,不要在处理过程中检查每个值的类型,而是在开始时根据每个列的数据类型准备一个不同的创建单元格方法。
我正在处理现有的 Excel 文件,该文件已经包含所有公式和格式,我将数据添加到 sheet 和 Table 然后当我在 Excel 中打开该文件我收到错误
"Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded. Removed Records: Cell information from /xl/worksheets/sheet6.xml part"
然后我打开了一个内容相同的手动创建的文件,它工作得很好。 我还发现了 Open XML 2.5 Productivity Tool,当我对生成的文件进行 运行 验证时,它说没有发现问题。
当我 运行 比较两个文件时,我看到生成的文件如下所示。
<x:c r="B462" t="inlineStr">
<x:is>
<x:t>1150828</x:t>
</x:is>
</x:c>
虽然手动创建的文件具有如下所示的单元格。
<c s="80" r="B462">
<v>
1150828
</v>
</c>
显然这里存在差异,但我不知道如何更正它,也不知道这种差异是否是错误的实际原因。但看到其他一切看起来都一样,我不知道它还能是什么。
哦,还有一些事情这个文件不起作用,但我可以使用另一个不包含 Table 的文件,当我合并 table 时,问题就出现了,所以我至少知道那么多。
此外,如果您要建议我使用 ClosedXML,请不要这样做。我已经使用过它,但由于某种我无法弄清楚的原因,它往往会随机停止格式化,因此我为什么要转向 OpenXML SDk
这是一些 C# 代码
dt.Load(reader);
RowCount = dt.Rows.Count;
ColumnCount = dt.Columns.Count;
workbookPart = spreadDoc.WorkbookPart;
SheetDimension sheetDimension = new SheetDimension() { Reference = "A1:" + ColumnLetters[ColumnCount - 1] + (RowCount + 1) };
worksheetPart = Program.GetWorksheetPart(workbookPart, reportStep.ExcelSheetName);
worksheetPart.Worksheet.SheetDimension = sheetDimension;
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
string relId = workbookPart.Workbook.Descendants<Sheet>().First(s => reportStep.ExcelSheetName.Equals(s.Name)).Id;
if (reportStep.ExcelTableExists)
{
TableDefinitionPart tableDef = null;
int looper = 0;
foreach (WorksheetPart wsp in spreadDoc.WorkbookPart.WorksheetParts)
{
if (wsp.TableDefinitionParts.Where(tbl => tbl.Table.DisplayName.Value.Equals(reportStep.ExcelTableName)).Count() == 1)
{
tableDef = spreadDoc.WorkbookPart.WorksheetParts.ElementAt(looper).TableDefinitionParts.Where(tbl => tbl.Table.DisplayName.Value.Equals(reportStep.ExcelTableName)).FirstOrDefault();
tableDef.Table.Reference.Value = "A1:" + (ColumnLetters[ColumnCount - 1] + (RowCount +1) ).ToString();
tableDef.Table.AutoFilter.Reference.Value = "A1:" + (ColumnLetters[ColumnCount - 1] + (RowCount +1)).ToString();
// tabledefinitionPart = Program.GetTablePart(wsp, reportStep.ExcelTableName, ColumnCount, RowCount);
}
looper++;
}
}
sheetData = Chef.Program.ExportDataTable(dt, sheetData);
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == reportStep.ExcelSheetName);
public static TableDefinitionPart GetTablePart(WorksheetPart worksheet, string tablename, int columnCount, int rowCount)
{
uint CellRange = (uint)(columnCount);
TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)(CellRange) };
var tableDefPart = worksheet.TableDefinitionParts.Where(tbl => tbl.Table.DisplayName.Value.Equals(tablename)).FirstOrDefault();
//worksheet.WorksheetPart.TableDefinitionParts.AddNewPart<TableDefinitionPart>(tablename);
var table = new Table() { HeaderRowCount = (uint)columnCount, Name = tablename, DisplayName = tablename, Reference = "A1:" + ColumnLetters[columnCount -1] + (rowCount + 1), TotalsRowShown = false };
TableStyleInfo tableStyleInfo1 = new TableStyleInfo()
{
Name = "TableStyleMedium2",
ShowFirstColumn = false,
ShowLastColumn = false,
ShowRowStripes = true,
ShowColumnStripes = false
};
table.Append(tableStyleInfo1);
// table.Append(tableColumns1);
tableDefPart.Table = table;
return tableDefPart;
}
编辑部分添加请求的其他方法更新于 2015 年 9 月 5 日
我确实删除了添加 header 值的代码,因为它们已经是 excel 文件基本模板的一部分。还删除了单元格数据类型的指定以保留模板已经将单元格数据类型设置为的内容。
public static SheetData ExportDataTable2(System.Data.DataTable exportData, SheetData sheetData)
{
//loop through each data row
DataRow contentRow;
int startRow = 2;
for (int i = 0; i < exportData.Rows.Count; i++)
{
contentRow = exportData.Rows[i];
sheetData.AppendChild(createContentRow(contentRow, i + startRow));
}
return sheetData;
}
private static Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
// cell.DataType = CellValues.Number;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
cell.CellValue = new CellValue(cellValue.ToString());
return cell;
}
private static Row createContentRow(DataRow dataRow, int rowIndex)
{
Row row = new Row
{
RowIndex = (UInt32)rowIndex
};
for (int i = 0; i < dataRow.Table.Columns.Count; i++)
{
Cell dataCell = createTextCell(i + 1, rowIndex, dataRow[i]);
// dataCell.DataType = CellValues.SharedString;
row.AppendChild(dataCell);
}
return row;
}
我还遇到了有关使用 OpenXml Sdk 的无效文件的问题。
查看 OpenXml Power Tools; they've solved all my issues :) Also you should switch to the OpenXml Sdk 2.6 以避免 System.IO.Packaging
出现问题。希望对您有所帮助!
好吧,看来您已使用以下示例 OpenXML SDK 2.0: Export a DataTable to Excel 作为代码的基础。这是创建单元格的原始代码:
private Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
cell.DataType = CellValues.InlineString;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
InlineString inlineString = new InlineString();
Text t = new Text();
t.Text = cellValue.ToString();
inlineString.AppendChild(t);
cell.AppendChild(inlineString);
return cell;
}
您的原始代码除了以下行外完全相同:
cell.DataType = CellValues.String;
看出区别了吗?
那你改成:
private static Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
// cell.DataType = CellValues.Number;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
cell.CellValue = new CellValue(cellValue.ToString());
return cell;
}
好的,问题是你没有正确设置cell.DataType
。它需要与单元格内容同步,否则你会从 Excel 得到这样的错误。在前一种情况下,您将内容设置为 inline string
,但数据类型设置为 String
。在后面 - 数据类型为 Number
(您是否注释了该行并不重要 - Number
是单元格的默认数据类型)但内容并不总是数字(相同的功能用于列标题 - 毕竟,它称为 createTextCell)。
要解决此问题,请使用示例中的原始代码,或此代码:
private static Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
cell.CellValue = new CellValue(cellValue.ToString());
return cell;
}
最后,如果您需要存储共享字符串、数字、日期等,请阅读文档并设置适当的属性。我同意 OpenXml API 不是很直观,但这就是我们所拥有的。
编辑: 根据您的评论,您的真正问题似乎并不完全是问题所在。以下是使用不同数据类型列导出 DataTable
的高性能示例:
public static class ExcelExporter
{
public static void ExportDataTable(DataTable table, SheetData data)
{
var cellFactory = new CellFactory[table.Columns.Count];
for (int i = 0; i < table.Columns.Count; i++)
cellFactory[i] = GetCellFactory(table.Columns[i].DataType);
int rowIndex = 0;
data.AppendChild(CreateHeaderRow(rowIndex++, table));
for (int i = 0; i < table.Rows.Count; i++)
data.AppendChild(CreateContentRow(rowIndex++, table.Rows[i], cellFactory));
}
private static Row CreateHeaderRow(int rowIndex, DataTable table)
{
var row = CreateRow(rowIndex);
for (int i = 0; i < table.Columns.Count; i++)
{
var cell = CreateTextCell(i, rowIndex, table.Columns[i].ColumnName);
row.AppendChild(cell);
}
return row;
}
private static Row CreateContentRow(int rowIndex, DataRow dataRow, CellFactory[] cellFactory)
{
var row = CreateRow(rowIndex);
for (int i = 0; i < dataRow.Table.Columns.Count; i++)
{
var cell = cellFactory[i](i, rowIndex, dataRow[i]);
row.AppendChild(cell);
}
return row;
}
private static Row CreateRow(int index) { return new Row { RowIndex = (uint)index + 1 }; }
private delegate Cell CellFactory(int columnIndex, int rowIndex, object cellValue);
private static CellFactory GetCellFactory(Type dataType)
{
CellFactory factory;
return CellFactoryMap.TryGetValue(dataType, out factory) ? factory : TextCellFactory;
}
private static readonly CellFactory TextCellFactory = CreateTextCell;
private static readonly CellFactory DateCellFactory = CreateDateCell;
private static readonly CellFactory NumericCellFactory = CreateNumericCell;
private static readonly CellFactory BooleanCellFactory = CreateBooleanCell;
private static readonly Dictionary<Type, CellFactory> CellFactoryMap = new Dictionary<Type, CellFactory>
{
{ typeof(bool), BooleanCellFactory },
{ typeof(DateTime), DateCellFactory },
{ typeof(byte), NumericCellFactory },
{ typeof(sbyte), NumericCellFactory },
{ typeof(short), NumericCellFactory },
{ typeof(ushort), NumericCellFactory },
{ typeof(int), NumericCellFactory },
{ typeof(uint), NumericCellFactory },
{ typeof(long), NumericCellFactory },
{ typeof(ulong), NumericCellFactory },
{ typeof(float), NumericCellFactory },
{ typeof(double), NumericCellFactory },
{ typeof(decimal), NumericCellFactory },
};
private static Cell CreateTextCell(int columnIndex, int rowIndex, object cellValue)
{
return CreateCell(CellValues.String, columnIndex, rowIndex, ToExcelValue(cellValue));
}
private static Cell CreateDateCell(int columnIndex, int rowIndex, object cellValue)
{
// NOTE: CellValues.Date is not supported in older Excel version.
// In all Excel versions dates can be stored with CellValues.Number and a format style.
// Since I have no styles, will export them just as text
//var cell = CreateCell(CellValues.Number, columnIndex, rowIndex, ToExcelDate(cellValue));
//cell.StyleIndex = ...;
//return cell;
return CreateCell(CellValues.String, columnIndex, rowIndex,
cellValue != null && cellValue != DBNull.Value ? ((DateTime)cellValue).ToShortDateString() : null);
}
private static Cell CreateNumericCell(int columnIndex, int rowIndex, object cellValue)
{
return CreateCell(CellValues.Number, columnIndex, rowIndex, ToExcelValue(cellValue));
}
private static Cell CreateBooleanCell(int columnIndex, int rowIndex, object cellValue)
{
// NOTE: CellValues.Boolean is not supported in older Excel version
//return CreateCell(CellValues.Boolean, columnIndex, rowIndex, ToExcelValue(cellValue));
return CreateCell(CellValues.String, columnIndex, rowIndex, ToExcelValue(cellValue));
}
private static Cell CreateCell(CellValues dataType, int columnIndex, int rowIndex, string cellValue)
{
var cell = new Cell();
if (dataType != CellValues.Number) cell.DataType = dataType;
cell.CellReference = GetColumnName(columnIndex) + (rowIndex + 1);
cell.CellValue = new CellValue(cellValue ?? string.Empty);
return cell;
}
private static string ToExcelValue(object value)
{
if (value == null || value == DBNull.Value) return null;
return Convert.ToString(value, CultureInfo.InvariantCulture);
}
private static DateTime ExcelBaseDate = new DateTime(1900, 1, 1);
private static string ToExcelDate(object value)
{
const int days29Feb1900 = 59;
if (value == null || value == DBNull.Value) return null;
var date = ((DateTime)value).Date;
var days = (date - ExcelBaseDate).Days + 1;
if (days >= days29Feb1900) days++;
return days.ToString(CultureInfo.InvariantCulture);
}
private static string GetColumnName(int index) { return ColumnNameTable[index]; }
private static readonly string[] ColumnNameTable = BuildColumnNameTable();
private static string[] BuildColumnNameTable()
{
var table = new string[16384];
var sb = new StringBuilder();
for (int i = 0; i < table.Length; i++)
table[i] = sb.BuildColumnName(i);
return table;
}
private static string BuildColumnName(this StringBuilder sb, int index)
{
const int startLetter = 'A';
const int letterCount = 'Z' - startLetter + 1;
sb.Clear();
while (true)
{
var letter = (char)(startLetter + (index % letterCount));
sb.Insert(0, letter);
if (index < letterCount) break;
index = (index / letterCount) - 1;
}
return sb.ToString();
}
}
关键是,不要在处理过程中检查每个值的类型,而是在开始时根据每个列的数据类型准备一个不同的创建单元格方法。