2013-03-21 108 views
3

我想將數據插入到多個工作表。對於我的Excel,我有兩張「圖表」和「ChartData」。我可以更新sheet2,chartdata表中的數據,但我無法將數據插入到sheet1。這裏是我試圖將數據插入Excel表格的代碼。這裏的數據來自數據庫。Excel文件與openxml與單張工作簿中的多個工作表

  double ticks = DateTime.Now.Ticks; 
     // MarketAnalysis ms = new MarketAnalysis(); 
     //ms.Marketanalysis(); 

     File.Copy(Srcpath, @"E:\Works\OpenXML\DownloadTemplates\ExcelGenerated" + ticks + ".xlsx", true); 
     using (SpreadsheetDocument myworkbok = SpreadsheetDocument.Open(@"E:\Works\OpenXML\DownloadTemplates\ExcelGenerated" + ticks + ".xlsx", true)) 
     { 
      //Acess the main workbook which contain all the references 

      WorkbookPart workbookpart = myworkbok.WorkbookPart; 
      //Get sheet by name 
      Sheet sheet = workbookpart.Workbook.Descendants<Sheet>().Where(s => s.Name == "ChartData").FirstOrDefault(); 

      //Worksheet Part by ID 
      WorksheetPart worksheetpart = workbookpart.GetPartById(sheet.Id) as WorksheetPart; 

      //Sheet data contains all the data 
      SheetData sheetdata = worksheetpart.Worksheet.GetFirstChild<SheetData>(); 
      DataSet ds = db.Chart1Data(); 

       for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
      { 
       // if (ds.Tables[0].Rows !=DBNull) 
       //{ 

       string Rowlabel = ds.Tables[0].Rows[i][0].ToString(); 
       // float? FY13Actuval = Convert.ToInt32(ds.Tables[0].Rows[i][1]); 

       if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i][1].ToString())) 
       { 
        // string s = ds.Tables[0].Rows[i][1].ToString(); 
        //FY13Actuval=float.Parse(ds.Tables[0].Rows[i][1].ToString()); 

        FY13Actuval = Convert.ToDouble(ds.Tables[0].Rows[i][1].ToString()); 
       } 
       else 
       { 

        FY13Actuval = null; 
       } 

       double? Budget = Convert.ToDouble(ds.Tables[0].Rows[i][2].ToString()); 
       double? Actuval = Convert.ToDouble(ds.Tables[0].Rows[i][3].ToString()); 
       Row contentrow = CreateContentRow(index, Product, Actual, Budget, Forecast); 

       index++; 
       sheetdata.AppendChild(contentrow); 

       // } 
      } 
.......Same code for the other 3 charts 
Sheet sheet1 = workbookpart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Charts").FirstOrDefault(); 

      WorksheetPart worksheetpart1 = workbookpart.GetPartById(sheet1.Id) as WorksheetPart; 

      //Sheet data contains all the data 
      SheetData sheetdata1 = worksheetpart1.Worksheet.GetFirstChild<SheetData>(); 

      DataSet dsTbl = db.Table(); 
      int SCMTblIndex=5; 
      for (int i = 0; i < dsTbl.Tables[0].Rows.Count; i++) 
      { 
       Row tblRow = CreateScorecardMetricTblRow(SCMTblIndex, dsTbl.Tables[0].Rows[i][0].ToString(), 
                    dsTbl.Tables[0].Rows[i][1].ToString(), 
                    dsTbl.Tables[0].Rows[i][2].ToString()); 
       SCMTblIndex++; 
       sheetdata1.AppendChild(tblRow); 
      } 

       myworkbok.WorkbookPart.Workbook.Save(); 
     } 

和方法來創建細胞:

public static string[] headerColumns = new string[]{ "A", "B", "C", "D", "E", "F", "G", "I", "J" }; 
public static string[] header = new string[] { "D", "E", "F" }; 

private static Row CreateContentRow(int index, string Product, double? Actual, double? Budget, double? Forecast) 
    { 

     //Create New ROw 
     Row r = new Row(); 
     r.RowIndex = (UInt32)index; 


     //Begin colums 
     Cell c0 = new Cell(); 
     c0.CellReference = headerColumns[0] + index; 
     CellValue v0 = new CellValue(); 
     v0.Text = Product; 
     c0.AppendChild(v0); 
     r.AppendChild(c0); 

     Cell c1 = new Cell(); 
     c1.CellReference = headerColumns[1] + index; 
     CellValue v1 = new CellValue(); 
     v1.Text = Actual.ToString(); 
     c1.AppendChild(v1); 
     r.AppendChild(c1); 

     Cell c2 = new Cell(); 
     c2.CellReference = headerColumns[2] + index; 
     CellValue v2 = new CellValue(); 
     v2.Text = Budget.ToString(); 
     c2.AppendChild(v2); 
     r.AppendChild(c2); 

     Cell c3 = new Cell(); 
     c3.CellReference = headerColumns[3] + index; 
     CellValue v3 = new CellValue(); 
     v3.Text = Forecast.ToString(); 
     c3.AppendChild(v3); 
     r.AppendChild(c3); 


     return r; 


    } 
public static Row CreateScorecardMetricTblRow(int index, string Act_Data, string Bud_Data, string VarPr_Data) 
    { 
     Row r = new Row(); 
     r.RowIndex = (UInt32)index; 
     //Begin Colums 

     Cell c0 = new Cell(); 
     c0.CellReference = header[0] + index; 
     CellValue v0 = new CellValue(); 
     v0.Text = Act_Data; 
     c0.AppendChild(v0); 
     r.AppendChild(c0); 

     Cell c1 = new Cell(); 
     c1.CellReference = header[1] + index; 
     CellValue v1 = new CellValue(); 
     v1.Text = Bud_Data; 
     c1.AppendChild(v1); 
     r.AppendChild(c1); 

     Cell c2 = new Cell(); 
     c2.CellReference = header[2] + index; 
     CellValue v2 = new CellValue(); 
     v2.Text = VarPr_Data; 
     c2.AppendChild(v2); 
     r.AppendChild(c2); 
      return r; 
    } 
+0

寫這些代碼,而不是聲明表片= Workbookpart.workbook .....等,寫這些變種片= workbookpart.Workbook.Descendants ()。凡(S => s.Name.Value。含有( 「薄片」)); (表單中的var表名) { if(sheetname.Name ==「Sheet1」) { } – 2013-03-25 13:08:25

回答

3

多張,以更新的只是

// Open the document for editing. 
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) 
{ 

    // Insert code here. 

} 

創建兩個類爲兩片,你需要更新表單元格的值。對於每個類的構造函數,通過SpreadsheetDocument對象和執行insert text into a cell in a spreadsheet document。只需避免插入新工作表的代碼並嘗試僅插入單元代碼。

0

你可以試試這個方法.....它可能有助於..

私有靜態細胞InsertCellInWorksheet(串列名,詮釋的rowIndex,WorksheetPart worksheetPart) { 工作表的工作表= worksheetPart.Worksheet; SheetData sheetData = worksheet.GetFirstChild(); string cellReference = columnName + rowIndex;

 Alignment alignment1 = new Alignment() { WrapText = true }; 

     // If the worksheet does not contain a row with the specified row index, insert one. 
     Row row; 
     row = new Row() { RowIndex = 3, StyleIndex = 1 }; 
     if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0) 
     { 
      row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First(); 
     } 
     else 
     { 
      row = new Row() { RowIndex = Convert.ToUInt32(rowIndex) }; 
      sheetData.Append(row); 
     } 

     // If there is not a cell with the specified column name, insert one. 
     if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0) 
     { 
      Row row2; 
      row2 = new Row() { RowIndex = 3, StyleIndex = 1 }; 
      Cell rCell = null; 
      foreach (Cell celld in row.Elements<Cell>()) 
      { 
       if (string.Compare(celld.CellReference.Value, "A3", true) > 0) 
       { 
        rCell = celld; 
        break; 
       } 
      } 

      // Add the cell to the cell table at A1. 
      Cell newCell = new Cell() { CellReference = "C3" }; 
      //Cell newCell1 = new Cell() { CellReference = "D3" }; 
      row.InsertBefore(newCell, rCell); 
      //row.InsertBefore(newCell1, rCell); 

      // Set the cell value to be a numeric value of 100. 
      newCell.CellValue = new CellValue("#GUIDeXactLCMS#"); 
      //newCell1.CellValue = new CellValue("EN"); 
      newCell.DataType = new EnumValue<CellValues>(CellValues.Number); 

      return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First(); 
     } 
     else 
     { 
      // Cells must be in sequential order according to CellReference. Determine where to insert the new cell. 
      Cell refCell = null; 
      foreach (Cell cell in row.Elements<Cell>()) 
      { 
       if (string.Compare(cell.CellReference.Value, cellReference, true) > 0) 
       { 
        //string val = cell.CellReference.Value; 
        refCell = cell; 
        break; 
       } 
      } 

      Cell newCell = new Cell() { CellReference = cellReference }; 
      row.InsertBefore(newCell, refCell); 


      return newCell; 


     } 
     worksheet.Save(); 
    } 
相關問題