0
数字を数字として扱う以外は、コードは正常に動作します。 ifステートメントは私がデバッグするときにdouble値をキャッチします。しかし、私はExcelに文字列以外のデータ型で書くことはできません。より正確にする必要がある場合、Excelのセルは正しいが、数値ではありません。C#でOpen XML SDKを使用してDataTableをExcelにエクスポートする際のデータ型を保存する
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();
if (dsrow[col].GetType() == typeof(Double))
{
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number;
}
else
{
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
}
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
の可能性のある重複した[OfficeオープンXML SDKの書き込み番号をシートに](https://stackoverflow.com/questions/16607063/office-open-xml -sdk-writing-numbers-to-sheet) – Kyra
数字の場合に重複として報告した質問の答えに見られるように、まず値を入れてからデータ型を変更します。それ以外の場合は、 'ToString'メソッド – Kyra