2015-06-25 41 views
1

我攜帶一個帶有兩個WorkSheet的Excel文件,要提供的第一個數據和包含數據透視表的第二個WorkSheet。在第一個工作表中插入數據並嘗試保存拋出異常。保存excel包的錯誤

例外: InneException { 「的cachesource不是一個工作表」} 消息 「保存文件時出錯C:\ Users \用戶idias \桌面\ Modelo.xlsx」

using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog()) 
     { 
      if (folderBrowserDialog.ShowDialog() == DialogResult.OK) 
      { 
       FileInfo fileInfo = new FileInfo(@"C:\Users\idias\Desktop\teste\Modelo.xlsx"); 
       if (!fileInfo.Exists) 
        throw new Exception("Arquivo Modelo não encontrado"); 

       string filename = string.Format("{0}\\{1}", folderBrowserDialog.SelectedPath, fileInfo.Name); 

       fileInfo.CopyTo(filename, true); 

       fileInfo = new FileInfo(filename); 

       using (ExcelPackage excelPackage = new ExcelPackage(fileInfo)) 
       { 
        ExcelWorkbook excelWorkBook = excelPackage.Workbook; 

        DataTable dtPlanoConta = Negocio.Financeiro.Relatorio.RecuperarPlanoConta(); 
        if (dtPlanoConta.Rows.Count > 0) 
        { 
         ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets[1]; 
         //Add some items in the cells... 
         int row = 3; 
         foreach (DataRow dr in dtPlanoConta.Rows) 
         { 
          row++; 

          //Campos           
          excelWorksheet.SetValue(row, 1, dr["ID"]); 
          excelWorksheet.SetValue(row, 2, dr["FATHER_ID"]); 
          excelWorksheet.SetValue(row, 3, dr["DESCRICAO_FORMATADA"]); 
         } 

         row = 1; 
         for (int i = 0; i < dtPlanoConta.Rows.Count; i++) 
         { 
          row++; 

          //Campos 
          excelWorksheet.Cells[row, 1].Style.Numberformat.Format = "@"; 
          excelWorksheet.Cells[row, 2].Style.Numberformat.Format = "@"; 
          excelWorksheet.Cells[row, 3].Style.Numberformat.Format = "@"; 
         } 

         excelWorksheet.Cells[excelWorksheet.Dimension.Address].AutoFitColumns(); 
        } 

        DataTable dtDemonstrativo = Negocio.Financeiro.Relatorio.RecuperarDemonstrativo(1, 3, "2015"); 
        if (dtDemonstrativo.Rows.Count > 0) 
        { 
         ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets[2]; 

         //Add some items in the cells... 
         int row = 1; 
         foreach (DataRow dr in dtDemonstrativo.Rows) 
         { 
          row++; 

          //Campos           
          excelWorksheet.Cells[row, 1].Value = dr["ID"]; 
          excelWorksheet.Cells[row, 2].Value = dr["OPERACAO"]; 
          excelWorksheet.Cells[row, 3].Value = dr["MES_ANO"]; 
          excelWorksheet.Cells[row, 4].Value = dr["VALOR_PLANEJADO"]; 
          excelWorksheet.Cells[row, 5].Value = dr["VALOR_REALIZADO"]; 
          excelWorksheet.Cells[row, 6].Value = dr["DIFERENCA_REAIS"]; 
          excelWorksheet.Cells[row, 7].Value = dr["DIFERENCA_PERCENTUAL"]; 
         } 

         row = 1; 
         for (int i = 0; i < dtDemonstrativo.Rows.Count; i++) 
         { 
          row++; 

          //Campos 
          excelWorksheet.Cells[row, 1].Style.Numberformat.Format = "@"; 
          excelWorksheet.Cells[row, 2].Style.Numberformat.Format = "@"; 
          excelWorksheet.Cells[row, 3].Style.Numberformat.Format = "DD/yyyy"; 
          excelWorksheet.Cells[row, 3].Style.Numberformat.Format = "#,##0.000"; 
          excelWorksheet.Cells[row, 4].Style.Numberformat.Format = "#,##0.000"; 
          excelWorksheet.Cells[row, 5].Style.Numberformat.Format = "#,##0.000"; 
          excelWorksheet.Cells[row, 6].Style.Numberformat.Format = "#,##0.000"; 
         } 

         excelWorksheet.Cells[excelWorksheet.Dimension.Address].AutoFitColumns(); 
        } 

        excelPackage.Save(); // This is the important part. 
       } 
      } 
     } 

回答

0

您應該添加ExcelWorksheet到ExcelWorkbook寫入單元格之前。 在their samples

+0

您好,Rui,感謝您的回覆。我做了你的建議,但仍然拋出同樣的錯誤。我正在使用現成的電子表格網站(http://www.daxpatterns.com/parent-child-hierarchies/) –

0

看看做了谷歌搜索的錯誤消息,並且看到了一些源代碼爲ExcelPackage(您可能會看到它here)在拋出異常。看起來,如果您修改數據透視表的源數據,則可能需要刷新/重新創建緩存源。檢查ExcelWorkSheet對象中的數據透視表 - 嘗試在保存文件之前調試其值。