2015-07-21 55 views
6

我創建使用LoadFromArrays使用EPPlus我怎麼能生成一個電子表格中的數字是數字而不是文本

數組的第一個條目是一個標題從List<object[]>電子表格,其他項都可能是數字,文本或日期(但對列表中的每個數組都是相同的)。

生成的Excel工作表具有綠色三角形警告,數字格式設置爲文本。通過所有的細胞

我環路,並設置其格式的號碼,例如ws.Cells[i, j].Style.Numberformat.Format = "0";

但是問題仍然存在,我仍然看到綠色的警告,即使數字格式設置爲數字,當我看在Format Cell...對話。

我在這裏有什麼選擇?我可以更多地瞭解每列中的類型,但是如何設置列標題?

有沒有比EPPlus更好的解決方案?或者我可以在下載電子表格之前進行一些更多後處理?

+1

通常這些綠色三角形來自存儲在字符串變量或屬性中的數字(代碼)。所以無論你如何設置excel,你都必須將字符串轉換爲代碼中的數字。情況會是這樣嗎? – Ernie

回答

8

由於您使用的對象數組它們可以包含數字和字符串,看起來像數字,你將不得不通過每個對象,並確定其類型:

[TestMethod] 
public void Object_Type_Write_Test() 
{ 
    //http://stackoverflow.com/questions/31537981/using-epplus-how-can-i-generate-a-spreadsheet-where-numbers-are-numbers-not-text 
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx"); 
    if (existingFile.Exists) 
     existingFile.Delete(); 

    //Some data 
    var list = new List<Object[]> 
    { 
     new object[] 
     { 
      "111.11", 
      111.11, 
      DateTime.Now 
     } 
    }; 

    using (var package = new ExcelPackage(existingFile)) 
    { 
     var ws = package.Workbook.Worksheets.Add("Sheet1"); 
     ws.Cells[1, 1, 2, 2].Style.Numberformat.Format = "0"; 
     ws.Cells[1, 3, 2, 3].Style.Numberformat.Format = "[$-F400]h:mm:ss\\ AM/PM"; 

     //This will cause numbers in string to be stored as string in excel regardless of cell format 
     ws.Cells["A1"].LoadFromArrays(list); 

     //Have to go through the objects to deal with numbers as strings 
     for (var i = 0; i < list.Count; i++) 
     { 
      for (var j = 0; j < list[i].Count(); j++) 
      { 

       if (list[i][j] is string) 
        ws.Cells[i + 2, j + 1].Value = Double.Parse((string) list[i][j]); 
       else if (list[i][j] is double) 
        ws.Cells[i + 2, j + 1].Value = (double)list[i][j]; 
       else 
        ws.Cells[i + 2, j + 1].Value = list[i][j]; 

      } 
     } 

     package.Save(); 
    } 
} 

通過上述,您會看到下面的圖片輸出請注意,用綠色箭頭的左上角單元格,因爲它是被寫了LoadFromArray一個字符串,它看起來像一個數字:

Excel Output

+0

對於這個夢幻般的幫助,一個滿意的答案和一個小小的勾號都是小小的補償......我真的在這個問題上大放異彩! – Loofer

+0

@Loofer Na,沒問題。很高興幫助。 – Ernie

1

我創建了一個擴展甲基od LoadFormulasFromArray,基於EPPlus LoadFromArray。該方法假定列表中的所有對象都被視爲公式(而不是LoadFromArray)。大圖是ValueFormula屬性取string而不是特定類型。我認爲這是一個錯誤,因爲無法區分字符串是Text還是Formula。實現Formula類型將啓用重載和類型檢查,從而可以始終做正確的事情。

// usage: ws.Cells[2,2].LoadFormulasFromArrays(MyListOfObjectArrays) 

public static class EppPlusExtensions 
{ 
    public static ExcelRangeBase LoadFormulasFromArrays(this ExcelRange Cells, IEnumerable<object[]> Data) 
    { 
     //thanx to Abdullin for the code contribution 
     ExcelWorksheet _worksheet = Cells.Worksheet; 
     int _fromRow = Cells.Start.Row; 
     int _fromCol = Cells.Start.Column; 
     if (Data == null) throw new ArgumentNullException("data"); 

     int column = _fromCol, row = _fromRow; 

     foreach (var rowData in Data) 
     { 
      column = _fromCol; 
      foreach (var cellData in rowData) 
      { 
       Cells[row, column].Formula = cellData.ToString(); 
       column += 1; 
      } 
      row += 1; 
     } 
     return Cells[_fromRow, _fromCol, row - 1, column - 1]; 
    } 
} 
相關問題