OpenXML - Excel文件未保存

时间:2015-05-21 18:33:23

标签: c# excel openxml

我尝试使用OpenXML编辑Excel电子表格,然后将其上传到网站。问题是:我在更新后保存了excel,如果我打开excel文件,数据就会被更改。但是当我尝试上传时,网站上说表格是空的(如果我手动打开电子表格并单击Ctrl + S并尝试上传,则上传工作正常。)所以我的猜测是我的程序不正确保存文件。谁知道为什么?

这是我的代码:

UpdateCell方法:

    String docName = "C:\\Temp\\MyFile.xlsx";
    public void UpdateCell(string text,
    uint rowIndex, string columnName)
    {
        // Open the document for editing.
        using (SpreadsheetDocument spreadSheet =
                 SpreadsheetDocument.Open(docName, true))
        {
            WorkbookPart wbPart = spreadSheet.WorkbookPart;
            WorksheetPart worksheetPart =  wbPart.WorksheetParts.First();

            if (worksheetPart != null)
            {
                Cell cell = GetCell(worksheetPart.Worksheet,
                                         columnName, rowIndex);

                cell.DataType = CellValues.InlineString;
                cell.InlineString = new InlineString(new Text(text.ToString()));
                // Save the worksheet.
                worksheetPart.Worksheet.Save();

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

    }

0 个答案:

没有答案