2014-09-19 18 views
3

我第一次對列應用粗體,但我只想要一行粗體而不是整列。所以我決定做同樣的事情,但使用row.RowStyle。編譯沒有錯誤,但我得到的是一個運行時錯誤,其中說r.RowStyle.SetFont(font);。我做了一個類,它做的一切出類拔萃相關的,它是這個班我得到這個錯誤(r.RowStyle.SetFont(font);):試圖對整行應用粗體,但不斷收到空引用 - NPOI

的NullReferenceException被處理

對象引用不設置到對象的實例。

調試整個過程,並且nothings null。我不明白爲什麼我在使用RowStyle時以及當我使用CellStyle時出現此錯誤,我沒有收到該錯誤。

這是我的課:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using NPOI.HSSF.UserModel; 
using NPOI.HPSF; 
using NPOI.POIFS.FileSystem; 
using NPOI.SS.UserModel; 

using System.IO; 


//works with everything related to excel 
namespace takeMyTime_text 
{ 
    class exelSheet 
    { 
      string exelPath; // where it's being saved 

     HSSFWorkbook wb2 = new HSSFWorkbook(); 

     ISheet sheet; 

     IRow r; 

     IFont font; 

     String[] headerTitles = { "Date", "In", "Out", "In", "Out", "Description" }; 

     // assing values to class variables 
     public void getValues(string path, string worksheetName) 
     { 
      exelPath = path; 
     } 


     //excel header 
     public void header() 
     { 
      #region set bold properties 

      font = wb2.CreateFont(); 

      font.FontHeightInPoints = 11; 

      font.FontName = "Arial"; 

      font.Boldweight = (short)FontBoldWeight.Bold; 

      #endregion 

      sheet = wb2.CreateSheet("test sheet"); 

      //se tiene que usar esto cada vez que vallas a escribir en el mismo row 
      r = sheet.CreateRow(0); 

      r.RowStyle.SetFont(font); 

      for (int i = 0; i < headerTitles.Length; i++) 
      { 
       r.CreateCell(i).SetCellValue(headerTitles[i]); 
      } 
     } 

     //excel footer 
     public void footer(int row, int col, string totalHours, int row2, int col2) 
     { 
      //ws.Cells[row, col] = new Cell("Worked hours:"); 

      //ws.Cells[row2, col2] = new Cell(totalHours); 

      //wb.Worksheets.Add(ws); 

      //wb.Save(exelPath);    
     } 

     // write the date on the excel file 
     public void writeDate(DateTime dt, int col, int row) 
     { 
      r = sheet.CreateRow(row); 

      r.CreateCell(col).SetCellValue(dt.Month + "/" + dt.Day + "/" + dt.Year);   
     } 

     //write and value on a cel 
     public void writeValues(string text, int col, int row) 
     { 
      //r = sheet.CreateRow(row); 

      r.CreateCell(col).SetCellValue(text);   
     } 

     //guarda la info en un excel 
     public void writeToFile() 
     { 
      FileStream file = new FileStream(exelPath, FileMode.Create); 

      wb2.Write(file); 

      file.Close(); 
     } 
    } 
} 
+0

我忘了提及我調試它,它不爲空。有點粗體值,字體名稱和高度 – user3842769 2014-09-19 20:18:56

+0

Nothing爲null。 null在哪裏?這是我需要知道的。我知道這個錯誤是如何工作的,但我不明白npoi的作品。 – user3842769 2014-09-19 20:29:56

回答

5

看起來你RowStyle爲空。嘗試以下操作:

var style = wb2.CreateCellStyle(); 
style.SetFont(font); 

r = sheet.CreateRow(0); 
r.RowStyle = style; 
+0

工作!但是我怎麼沒有工作? – user3842769 2014-09-19 21:16:49

+0

@ user3842769 Pierre-Luc Pineault發佈了一個很好的鏈接,可以很好地解釋這個問題。您的行的RowStyle屬性未初始化,因此在其上調用SetFont方法會引發異常。在上面的示例中,我先創建一個樣式對象,然後將其分配給該行的RowStyle屬性。 – 2014-09-20 00:54:28

相關問題