2012-05-16 72 views
2

您好所有我讀使用Apche POIXSSF一個xlsx文件。現在我想讀取單元格的顏色並在新的xlsx文件上應用相同的顏色。我將如何做到這一點。我的代碼是:閱讀XLSX文件時apche的POI獲取單元格顏色

public void readXLSXFile(String filePath) throws FileNotFoundException, IOException 
    { 
     XSSFRow row; 
     XSSFRow new_row; 
     XSSFSheet sheet; 
     XSSFCell cell; 
     XSSFCell new_cell; 
     XSSFCellStyle cellStyle; 
     XSSFDataFormat dataFormat; 
     XSSFColor color; 

     XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath)); 
     XSSFWorkbook workbook = new XSSFWorkbook(); 
     XSSFSheet new_sheet = (XSSFSheet) workbook.createSheet(); 
     for(int i = 0; i < xssfWorkbook.getNumberOfSheets(); i++) 
     { 
      sheet = xssfWorkbook.getSheetAt(i); 
      for(int j =0; j<sheet.getLastRowNum(); j++) 
      { 
       row = (XSSFRow) sheet.getRow(j); 
       new_row = new_sheet.createRow(j); 
       for(int k = 0; k<row.getLastCellNum(); k++) 
       { 
        cell = row.getCell(k); 
        new_cell = new_row.createCell(k); 
        cellStyle = workbook.createCellStyle(); 
        dataFormat = workbook.createDataFormat(); 
        cellStyle.setDataFormat(dataFormat.getFormat(cell.getCellStyle().getDataFormatString())); 
        color = cell.getCellStyle().getFillBackgroundColorColor(); 
        cellStyle.setFillForegroundColor(color); 
        new_cell.setCellStyle(cellStyle); 
        System.out.println(cell.getCellStyle().getFillForegroundColor()+"#"); 
        switch (cell.getCellType()) { 
        case 0: 
         new_cell.setCellValue(cell.getNumericCellValue()); 
         break; 
        case 1: 
         new_cell.setCellValue(cell.getStringCellValue()); 
         break; 
        case 2: 
         new_cell.setCellValue(cell.getNumericCellValue()); 
         break; 
        case 3: 
         new_cell.setCellValue(cell.getStringCellValue()); 
         break; 
        case 4: 
         new_cell.setCellValue(cell.getBooleanCellValue()); 
         break; 
        case 5: 
         new_cell.setCellValue(cell.getErrorCellString()); 
         break; 
        default: 
         new_cell.setCellValue(cell.getStringCellValue()); 
         break; 
        } 
       } 
      } 
     } 
     workbook.write(new FileOutputStream("G:\\lalit.xlsx")); 
    } 

我正在使用Apche POI 3.8。

+0

我有一個類似的問題我自己:http://stackoverflow.com/questions/18112155/access-to-the-color-palette-in-an-xssfworkbook我也得到64的索引值背景,我很確定這不是一個有效的價值。我爲橙色和黑色都得到它。 –

回答

5

我貼到vikiiii的答案評論。我認爲我會更多地擴展它。他的回答是特定於HSSF(.xls的),但無論是HSSF和XSSF類都是從同一個接口下降使代碼是一樣的,你只需要使用XSSF代替HSSF的。看到你想重用的顏色我推薦使用:

XSSFColor bgColor = xssfCell.getCellStyle().getFillBackgroundColorColor(); 

適用於Javadoc見here。現在要設置一個新的單元格爲該顏色,您可以使用this

secondCell.getCellStyle().setFillBackgroundColor(bgColor); 

我建議你看的是XSSF和HSSF類從下降並看看讓你的代碼能夠同時處理XLS和XLSX文件的接口。據我所知,唯一的區別是你設置工作簿的方式,使用WorkbookFactory

2

您可以使用此代碼來獲取單元格顏色。

cell.getCellStyle().getFillBackgroundColor(); 

嘗試

HSSFColor.getIndexHash().get(myCell.getCellStyle().getFillBackgroundColor()) 
+0

謝謝你,但我已經準備好在你的代碼中使用它了。但它不起作用。 –

+0

我認爲它會給你一個數字,對吧? – vikiiii

+0

是的,它返回數字64. –

相關問題