1

我有以下代码,它没有将值保存到单元格和文件中。它在字段中显示值,cell.cellvalue但没有将其写入 excel。我不知道如何保存文件。我使用了 OpenXml-SDK,并将datatable值写入每个cell/row创建的电子表格文档。

 using (SpreadsheetDocument ssd=SpreadsheetDocument.Open(Server.MapPath(@"\ExcelPackageTemplate.xlsx"),true))
        {
            WorkbookPart wbPart = ssd.WorkbookPart;
            WorksheetPart worksheetPart = wbPart.WorksheetParts.First();

            SheetData sheetdata = worksheetPart.Worksheet.GetFirstChild<SheetData>();
            string[] headerColumns = new string[] { dt.Columns[0].ColumnName, dt.Columns[1].ColumnName,dt.Columns[2].ColumnName };
            DocumentFormat.OpenXml.Spreadsheet.Row r = new DocumentFormat.OpenXml.Spreadsheet.Row();
            int RowIndexer = 1;
            //int colInd=0;
            r.RowIndex = (UInt32)RowIndexer;

            string test = ColumnName(RowIndexer);

            foreach (DataColumn dc in dt.Columns)
            {

                DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                cell.CellReference = test+RowIndexer;
                cell.DataType = CellValues.InlineString;
                cell.InlineString = new InlineString(new Text(dc.ColumnName.ToString()));
                DocumentFormat.OpenXml.Spreadsheet.CellValue value = new DocumentFormat.OpenXml.Spreadsheet.CellValue();

                r.AppendChild(cell);

                // colInd++;
            }

            //r.RowIndex = (UInt32)RowIndexer;
            RowIndexer = 2;
            foreach (DataRow dr in dt.Rows)
            {
                DocumentFormat.OpenXml.Spreadsheet.Row Row = new DocumentFormat.OpenXml.Spreadsheet.Row();
                string Index = ColumnName(RowIndexer);
                Row.RowIndex = (UInt32)RowIndexer;
                foreach (object value in dr.ItemArray)
                {
                    DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                    cell.DataType = CellValues.InlineString;
                    cell.InlineString =  new InlineString(new Text(value.ToString()));
                    cell.CellReference = Index+RowIndexer;
                   // cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value.ToString());
                    Row.AppendChild(cell);

                }
                RowIndexer++;

            }

            worksheetPart.Worksheet.Save();
            wbPart.Workbook.Save();
            ssd.Close();
4

2 回答 2

5

尝试这个:

using (SpreadsheetDocument ssd = SpreadsheetDocument.Open(Server.MapPath(@"\ExcelPackageTemplate.xlsx"), true))
{
    WorkbookPart wbPart = ssd.WorkbookPart;
    WorksheetPart worksheetPart = wbPart.WorksheetParts.First();

    SheetData sheetdata = worksheetPart.Worksheet.GetFirstChild<SheetData>();
    string[] headerColumns = new string[] { dt.Columns[0].ColumnName, dt.Columns[1].ColumnName, dt.Columns[2].ColumnName };
    DocumentFormat.OpenXml.Spreadsheet.Row r = new DocumentFormat.OpenXml.Spreadsheet.Row();
    DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
    int RowIndexer = 1;
    int ColumnIndexer = 1;

    r.RowIndex = (UInt32)RowIndexer;
    foreach (DataColumn dc in dt.Columns)
    {
        cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
        cell.CellReference = ColumnName(ColumnIndexer) + RowIndexer;
        cell.DataType = CellValues.InlineString;
        cell.InlineString = new InlineString(new Text(dc.ColumnName.ToString()));
        // consider using cell.CellValue. Then you don't need to use InlineString.
        // Because it seems you're not using any rich text so you're just bloating up
        // the XML.

        r.AppendChild(cell);

        ColumnIndexer++;
    }
    // here's the missing part you needed
    sheetdata.Append(r);

    RowIndexer = 2;
    foreach (DataRow dr in dt.Rows)
    {
        r = new DocumentFormat.OpenXml.Spreadsheet.Row();
        r.RowIndex = (UInt32)RowIndexer;
        // this follows the same starting column index as your column header.
        // I'm assuming you start with column 1. Change as you see fit.
        ColumnIndexer = 1;
        foreach (object value in dr.ItemArray)
        {
            cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
            // I moved it here so it's consistent with the above part
            // Also, the original code was using the row index to calculate
            // the column name, which is weird.
            cell.CellReference = ColumnName(ColumnIndexer) + RowIndexer;
            cell.DataType = CellValues.InlineString;
            cell.InlineString = new InlineString(new Text(value.ToString()));

            r.AppendChild(cell);
            ColumnIndexer++;
        }
        RowIndexer++;

        // missing part
        sheetdata.Append(r);
    }

    worksheetPart.Worksheet.Save();
    wbPart.Workbook.Save();
    ssd.Close();
}

一些评论:

  • ColumnName() 函数来自这里
  • 我假设您想要一行中的列标题,以及后续行中的数据(因为原始代码具有用于计算列名的行索引)。
  • 清理了代码的某些部分,使其更易于阅读,并且列标题和数据行部分的书写风格更加一致。
  • 我建议您考虑使用 CellValue 而不是 InlineString。根据您的代码,您正在导入一个 DataTable,并且您似乎不需要富文本,因此 InlineString 有点矫枉过正,可能会使生成的文件更大(臃肿的 XML)。使用其中一种,并记住相应地设置 DataType。
  • 此外,该代码仅适用于完全空的 SheetData。
于 2013-06-20T07:07:33.783 回答
2

这里有一些可能的问题:

  • 您创建并添加新的行和单元格:它假定工作表在您打开时是完全空白的(即:没有具有相同索引/地址的行或单元格)。

  • 既然你设置了CellTypeto InlineString,你需要设置Cell.InlineString而不是Cell.CellValue

    cell.DataType = CellValues.InlineString;
    cell.InlineString = new InlineString(New Text(value.TsString()));
    
  • 此外,您的代码中关于 CellReference. 这些部分代码没有意义:

    cell.CellReference = dc.ColumnName.ToString();
    cell.CellReference = value.ToString();
    

    单元格地址应该是这样的“A1”。您必须检查您的代码以设置正确的地址。您已经有了 rowIndex。您需要获取列名。这 将列索引转换为 Excel 列名称可以提供帮助。

于 2013-06-19T16:55:10.063 回答