【问题标题】:Inserting to existing Excel file with OpemXML and C# is causing Excel problems with content and forcing repairs使用 OpenXML 和 C# 插入现有 Excel 文件会导致 Excel 出现内容问题并强制修复
【发布时间】:2016-05-12 01:47:59
【问题描述】:

我能够在没有错误或异常的情况下运行以下代码(在代码方面)

private ExcelRow CreateContentRow(int index, StockHolder stockholder)
{
    ExcelRow r = new ExcelRow();
    r.RowIndex = (UInt32)index + 1;
    int i = 0;

    string[] headerColumns = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K" };
    var eraseThis = stockholder.GetType().GetProperties().Count();
    foreach (var prop in stockholder.GetType().GetProperties())
    {
        ExcelCell c = new ExcelCell();
        c.CellReference = headerColumns[i].ToString() + index;

        if (prop.PropertyType.ToString().Equals("System.string", StringComparison.InvariantCultureIgnoreCase))
        {
            var result = prop.GetValue(stockholder, null);

            if (result == null)
            {
                result = "";
            }

            c.DataType = CellValues.String;
            InlineString inlineString = new InlineString();
            Text t = new Text();
            t.Text = result.ToString();
            inlineString.AppendChild(t);
            c.AppendChild(inlineString);
        }

    //Other "if" statements here for checking bool and numeric (similar to the above "if" statement, but none of them are used at this time

    return r;
}

“索引”参数是我尝试添加到的工作表中的行数(它似乎能够读取工作表名称(因为无论如何只有一个)),而“Stockholder”参数是自定义的填充数据的对象(全部不为空)

问题是当我尝试像这样打开 Excel 文档时

当我点击是时,它会显示这个弹出窗口

我尝试搜索时的结果对于另一个 3rd 方库(以“ERPPS”或类似名称开头)更有用。调试时,属性似乎正确映射(使用另一种映射方法,无论如何它们都是字符串)。任何帮助或链接将不胜感激,因为我已经为此工作了一段时间。不,此时使用网络应用程序或表单不是替代方案(很遗憾,就像很久以前一样)。谢谢

编辑 - 另外,这里是调用方法(因为它也可能是它的一部分)

private void InsertIntoExistingExcel(StockHolder sh)
{
    using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(locationOfExcelFile, true))
    {
        WorkbookPart workbookPart = myWorkbook.WorkbookPart;
        var stockHolderInfo_Sheet = myWorkbook.WorkbookPart.Workbook.Sheets.GetFirstChild<Sheet>();
        IEnumerable<Sheet> Sheets = new List<Sheet>() { stockHolderInfo_Sheet };
        if (Sheets.Count() > 0)
        {
            string relationshipId = Sheets.First().Id.Value;
            WorksheetPart worksheetPart = (WorksheetPart)myWorkbook.WorkbookPart.GetPartById(relationshipId);
            SheetData sheetdata = worksheetPart.Worksheet.GetFirstChild<SheetData>();
            var sheetDataCount = sheetdata.Count();
            var newContentRow = CreateContentRow(sheetDataCount, sh);
            sheetdata.AppendChild(newContentRow);
            workbookPart.Workbook.Save();
        }
    }
}

【问题讨论】:

  • 您是否确认您添加的所有数据实际上都显示在工作簿中?我已经多次收到类似的错误。有几个不同的原因,但最常见的是我添加的数据不在 Excel 预期的文档中。所以 Excel 会忽略一些不合适的数据。 (我可以看到这种情况发生在您将行附加到工作表的末尾。)
  • 不确定.. 我(发布后不久)去了另一条路。我已将其发布为下面的答案

标签: c# excel openxml


【解决方案1】:

解决了!或者更确切地说,使用了不同的方法

    private void interopExcelInsertion(StockHolder sh, int rowNumberToUpdate)
    {
        ExcelInterop.Application oApp;
        ExcelInterop.Worksheet oSheet;
        ExcelInterop.Workbook oBook;

        oApp = new ExcelInterop.Application();
        oBook = oApp.Workbooks.Open(locationAndNameOfExcelFile);
        oSheet = oBook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

        var rowToUpdate = rowNumberToUpdate > 0 ? rowNumberToUpdate : oSheet.Cells.SpecialCells(ExcelInterop.XlCellType.xlCellTypeLastCell, System.Type.Missing).Row;
        int i = 1;
        foreach (var prop in sh.GetType().GetProperties())
        {
            var result = prop.GetValue(sh, null);
            oSheet.Cells[rowToUpdate + 1, i] = result == null ? "" : result.ToString();
            i++;
        }

        oBook.Save();
        oBook.Close();
        oApp.Quit();
        //Marshal.ReleaseComObject(oApp);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多