使用OpenXML将DataSet转换为Excel

时间:2014-02-24 16:55:33

标签: c# excel

我有一个DataSet,上面有几个表。我想将它导出到Excel,其中每个表将在不同的工作表上。我怎样才能使用OpenXML,但我想保留列的DataType(例如:字符串,日期,数字等)? 目前我有:

using (SpreadsheetDocument workbook = SpreadsheetDocument.Create(fileName,
    SpreadsheetDocumentType.Workbook)){
workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new Workbook {Sheets = new Sheets()};

uint sheetId = 1;

var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
sheetPart.Worksheet = new Worksheet(sheetData);

var sheets = workbook.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);

if (sheets.Elements<Sheet>().Any())
{
    sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}

var sheet = new Sheet
{
    Id = relationshipId,
    SheetId = sheetId,
    Name = exportedSheet
};
sheets.Append(new List<OpenXmlElement> {sheet});

var headerRow = new Row();

var columns = new List<string>();
foreach (DataColumn column in extractedData.Columns)
{
    columns.Add(column.ColumnName);

    var cell = new Cell
    {
        DataType = CellValues.String,
        CellValue = new CellValue(column.ColumnName)
    };
    headerRow.AppendChild(cell);
}

sheetData.AppendChild(headerRow);

foreach (DataRow dsrow in extractedData.Rows)
{
    var newRow = new Row();
    DataRow dsrow1 = dsrow;
    foreach (Cell cell in columns.Select(col => new Cell
    {
        DataType = CellValues.String,
        CellValue = new CellValue(dsrow1[col].ToString())
    }))
    {
        newRow.AppendChild(cell);
    }

    sheetData.AppendChild(newRow);
}}

1 个答案:

答案 0 :(得分:2)

您只需将每列的值映射到相应的CellValues值即可。这是一个粗略的想法。

// store this somewhere (a static field?)
var columnTypeToCellDataTypeMap = new Dictionary<Type, CellValues>
    {
        { typeof(Boolean), CellValues.Boolean },
        { typeof(Byte), CellValues.Number },
        { typeof(Decimal), CellValues.Number },
        { typeof(Char), CellValues.String },
        { typeof(DateTime), CellValues.Date },
        { typeof(Double), CellValues.Number },
        { typeof(Decimal), CellValues.Number },
        { typeof(Int16), CellValues.Number },
        { typeof(Int32), CellValues.Number },
        { typeof(Int64), CellValues.Number },
        { typeof(SByte), CellValues.Number },
        { typeof(Single), CellValues.Number },
        { typeof(String), CellValues.String },
        { typeof(UInt16), CellValues.Number },
        { typeof(UInt32), CellValues.Number },
        { typeof(UInt64), CellValues.Number },
    }

// and use it like this:
foreach (DataRow row in table.Rows)
{
    foreach (DataColumn column in table.Columns)
    {
        // map the column type to an OpenXML SDK CellValues type
        CellValues cellDataType;
        if (!columnTypeToCellDataTypeMap.TryGetValue(column.DataType, out cellDataType))
            cellDataType = CellValues.String;

        // get the cell value
        object value = row[column];
        string cellValue = (value != null ? value.ToString() : "");

        // construct the cell
        var cell = new Cell { DataType = cellDataType, CellValue = value };

        // etc
        ...
    }
}