2010-08-06 129 views
2

我想在使用NPOI庫的Excel中創建雙精度和數字格式單元格。我用這樣的代碼NPOI Excel數字格式不顯示在asp.net中的Excel工作表中

Dim cell As HSSFCell = row.CreateCell(j) 
cell.SetCellValue(Double.Parse(dr(col).ToString)) 

在Excel中數字排列正確的,但是當我在「常規」檢查格式,它顯示

alt text

然後我改變了我的代碼,如下圖所示

Dim cell As HSSFCell = row.CreateCell(j) 
cell.SetCellValue(Double.Parse(dr(col).ToString)) 
Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle 
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,#0.0") 
cell.CellStyle = cellStyle 

然後,當打開文件時,它給出了錯誤,也花了很長時間打開。但Excel格式顯示在「數字」

錯誤顯示如下。

alt text

如何解決這一問題?

+0

您是否使用NPOI 1.6或更高? – James 2010-08-06 16:58:53

+0

1.2.3是最新版本,據我所知:http://npoi.codeplex.com/releases/view/49524 – MikeWyatt 2010-08-30 19:29:48

回答

4

看看this,你是否爲每個單元格創建了一個cellStyle對象?如果不這樣做。嘗試在創建單元格之前創建幾個樣式,然後將這些預定義樣式應用於您創建的單元格。

0

Hare是一種在Excel中創建雙格式文件的簡單方法使用NPOI

//make NUMERIC Format in Excel Document // Author: Akavrelishvili 
    var eRow = sheet.CreateRow(rowIndex); //create new Row , rowIndex - it's integer, like : 1,2,3 
    eRow.CreateCell(0).SetCellValue(row["ProvidName"].ToString()); //create cell and set string value 

    double Amount = Convert.ToDouble(row["Amount"].ToString()); //convert string to double 
    eRow.CreateCell(1).SetCellValue(Amount); // create cell and set double value. 

這是工作版本,我用了很多項目。

很難在Excel中插入日期時間格式,在互聯網上沒有好的例子,我認爲它有助於人們正確地做到這一點。 我給你的代碼示例:

 //make Date Time Format in Excel Document // Author: Akavrelishvili 

變種eRow = sheet.CreateRow(rowIndex位置); //創建新行// rowIndex位置 - 這是整數,如:1,2,3

ICellStyle cellDateStyle = workBook.CreateCellStyle(); //create custom style 
cellDateStyle.DataFormat = workBook.CreateDataFormat().GetFormat("dd/mm/yyyy"); //set day time Format 

eRow.CreateCell(3).SetCellValue(Convert.ToDateTime(row["Date"])); //set DateTime value to cell 
        eRow.GetCell(6).CellStyle = cellDateStyle; // Restyle cell using "cellDateStyle" 


I hope it helps 
+0

workBook.CreateDataFormat()。GetFormat();確實是這樣做的正確方法,並沒有與GetBuiltinFormat() – kmdsax 2015-05-07 18:31:53

+0

有任何運氣我已經完成了解決方案,它使用這種方式來製作正確的日期時間格式,在這一刻解決方案工作沒有問題。我認爲這是一個最好的辦法。 – 2015-05-21 06:37:40

0

要解決的太多不同的單元格樣式聲明,你可能正在運行的循環之外的所有樣式。

我假設你'j'將是枚舉數,所以我會放棄你在一個正確的格式爲你。

Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle 
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,#0.0") 

For col = 0 To ColoumCounter 
    For j = 0 To Counter 
    Dim cell As HSSFCell = row.CreateCell(j) 
    cell.SetCellValue(Double.Parse(dr(col).ToString)) 
    cell.CellStyle = cellStyle 
    Next 
Next 

這應該會更好一些,通過限制「新」風格的數量。

0

創建一個樣式,然後但這樣式列

ICellStyle _TextCellStyle = wb1.CreateCellStyle(); 

_TextCellStyle.DataFormat = wb1.CreateDataFormat().GetFormat("@"); 
sheet.SetDefaultColumnStyle(2, _TextCellStyle); 
+0

歡迎來到SO。請訪問https://stackoverflow.com/tour – 2018-03-08 08:56:52