2013-05-17 9 views
6

Sto provando a scrivere numeri da un DataTable a un foglio dati - sfortunatamente, questo non funziona come previsto, e. g. il DataSheet è corrotto.Office Open XMl SDK Scrittura di numeri su foglio

Sto usando il seguente codice:

private void AddDataToSheet(ExcelViewData data, SheetData sheetData) 
      { 
       var excelData = data.WriteableDataTable; 
//// this returns a datatable 
////the numbers have a format like "8,1" "8,0" etc. 
       for (int i = 0; i < excelData.Rows.Count; i++) 
       { 
        Row row = new Row(); 
        //row.RowIndex = (UInt32)i; 
        for (int c = 0; c < excelData.Columns.Count; c++) 
        { 
         Cell cell = new Cell(); 
         CellValue cellvalue = new CellValue(); 
         //cell.CellReference = SharedMethods.GetExcelColumnName(i + 1) + (c + 1).ToString(); 
         cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number; 
         cellvalue.Text = excelData.Rows[i][c].ToString().Replace(",","."); 
         cell.Append(cellvalue); 
         row.Append(cell); 
        } 

        sheetData.Append(row); 
       } 
      } 

Qualsiasi idea del perché questo non riesce? Ho appreso più tutorial con lo stesso approccio.

risposta

4

provare questo metodo:

public void InsertDataTableIntoExcel(SpreadsheetDocument _excelDoc, SheetData SheetData, DataTable excelData, int rowIndex = 1) 
    { 
     if (_excelDoc != null && SheetData != null) 
     { 
      if (excelData.Rows.Count > 0) 
      { 
       try 
       { 
        uint lastRowIndex = (uint)rowIndex; 
        for (int row = 0; row < excelData.Rows.Count; row++) 
        { 
         Row dataRow = GetRow(lastRowIndex, true); 
         for (int col = 0; col < excelData.Columns.Count; col++) 
         { 
          Cell cell = GetCell(dataRow, col + 1, lastRowIndex); 

          string objDataType = excelData.Rows[row][col].GetType().ToString(); 
          //Add text to text cell 
          if (objDataType.Contains(TypeCode.Int32.ToString()) || objDataType.Contains(TypeCode.Int64.ToString()) || objDataType.Contains(TypeCode.Decimal.ToString())) 
          { 
           cell.DataType = new EnumValue<CellValues>(CellValues.Number); 
           cell.CellValue = new CellValue(objData.ToString()); 
          } 
          else 
          { 
           cell.CellValue = new CellValue(objData.ToString()); 
           cell.DataType = new EnumValue<CellValues>(CellValues.String); 
          } 
         } 
         lastRowIndex++; 
        } 
       } 
       catch (OpenXmlPackageException ex) 
       { 
        throw ex; 
       } 
       catch (Exception ex) 
       { 
        throw ex; 
       } 
      } 
      else 
      { 
       OpenXmlPackageException openEx = new OpenXmlPackageException("No data from datatable"); 
       throw openEx; 
      } 
     } 
     else 
     { 
      OpenXmlPackageException openEx = new OpenXmlPackageException("Workbook not found"); 
      throw openEx; 
     } 
    } 
+0

Grazie per il codice - unfortuanetly, sto iniziando ad un foglio perfettamente vuoto, non ci sono righe da scrivere. Proverò il codice interno - sembra molto promettente! –

+0

è un metodo nuovo che ha funzionato bene, è possibile utilizzare il codice in cui si crea la cella e aggiungere il valore della cella e CellType non è altro che la formattazione di Cell. Spero che questo dia la tua risposta. – KirtiSagar

+0

Ha funzionato, grazie! :) –