Java操作EXCEL的利器一般都是POI和JXL,鄙人只是POI的忠實粉絲。(其實我是沒有用過JXL)。
現在大多數的excel都是07以上的版本,所以我一般是用07的基礎上使用POI。
單元格有樣式和值,以及值得類型。
樣式復制封裝成一個函數:
public XSSFCellStyle cloneAllCellStyle(XSSFCell sourceCell, XSSFWorkbook targetWb){
//創建一個樣式
XSSFCellStyle tempStyle = targetWb.createCellStyle(); //樣式
//數值格式,創建字符及數字格式
DataFormat format= targetWb.createDataFormat();
//字體
XSSFFont font= targetWb.createFont();
try{
tempStyle.setDataFormat(format.getFormat( sourceCell.getCellStyle().getDataFormatString()));
}catch(NullPointerException e){
tempStyle.setDataFormat((short)0);
}
font.setColor(sourceCell.getCellStyle().getFont().getXSSFColor());
font.setBold(sourceCell.getCellStyle().getFont().getBold());
font.setBoldweight(sourceCell.getCellStyle().getFont().getBoldweight());
try{
font.setCharSet(sourceCell.getCellStyle().getFont().getCharSet());
}catch(POIXMLException e){
font.setCharSet(0);
}
// font.setCharSet(sourceCell.getCellStyle().getFont().getCharSet());
font.setFamily(sourceCell.getCellStyle().getFont().getFamily());
font.setFontHeight(sourceCell.getCellStyle().getFont().getFontHeight());
font.setFontHeightInPoints(sourceCell.getCellStyle().getFont().getFontHeightInPoints());
font.setFontName(sourceCell.getCellStyle().getFont().getFontName());
font.setItalic(sourceCell.getCellStyle().getFont().getItalic());
font.setStrikeout(sourceCell.getCellStyle().getFont().getStrikeout());
// font.setThemeColor(sourceCell.getCellStyle().getFont().getThemeColor());
font.setTypeOffset(sourceCell.getCellStyle().getFont().getTypeOffset());
font.setUnderline(sourceCell.getCellStyle().getFont().getUnderline());
tempStyle.setAlignment( sourceCell.getCellStyle().getAlignment());
tempStyle.setVerticalAlignment(sourceCell.getCellStyle().getVerticalAlignment());
tempStyle.setBorderBottom(sourceCell.getCellStyle().getBorderBottom());
tempStyle.setBorderLeft(sourceCell.getCellStyle().getBorderLeft());
tempStyle.setBorderRight(sourceCell.getCellStyle().getBorderRight());
tempStyle.setBorderTop(sourceCell.getCellStyle().getBorderTop());
tempStyle.setBottomBorderColor(sourceCell.getCellStyle().getBottomBorderXSSFColor());
tempStyle.setLeftBorderColor(sourceCell.getCellStyle().getLeftBorderXSSFColor());
tempStyle.setRightBorderColor(sourceCell.getCellStyle().getRightBorderXSSFColor());
tempStyle.setTopBorderColor(sourceCell.getCellStyle().getTopBorderXSSFColor());
tempStyle.setFillBackgroundColor(sourceCell.getCellStyle().getFillBackgroundColorColor());
tempStyle.setFont(font);
try{
tempStyle.setFillForegroundColor(sourceCell.getCellStyle().getFillForegroundColorColor());
}catch(NullPointerException e){
tempStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
}
tempStyle.setFillPattern(sourceCell.getCellStyle().getFillPattern());
tempStyle.setRotation(sourceCell.getCellStyle().getRotation());
tempStyle.setHidden(sourceCell.getCellStyle().getHidden());
tempStyle.setWrapText(sourceCell.getCellStyle().getWrapText());
tempStyle.setIndention(sourceCell.getCellStyle().getIndention());
tempStyle.setLocked(sourceCell.getCellStyle().getLocked());
return tempStyle;
}
調用直接獲取單元格的樣式內容。
獲取單元格值的類型:cell.getCellType()
根據值類型不同獲取不同的值:
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
tempValue.add("");
break;
case Cell.CELL_TYPE_BOOLEAN:
tempValue.add(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
tempValue.add(cell.getErrorCellString());
break;
case Cell.CELL_TYPE_FORMULA:
tempValue.add(cell.getCellFormula());
map.put("formulaFlag", true);
break;
case Cell.CELL_TYPE_NUMERIC:
tempValue.add(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
tempValue.add(cell.getStringCellValue());
break;
default:
break;
}
創建內容
//工作空間
XSSFWorkbook targetWb = new XSSFWorkbook();
//sheet
XSSFSheet targetSheet = targetWb.createSheet("行匯總");
// 刪除sheet
targetWb.removeSheetAt(index); //index表示第幾個sheet,從0開始計數
//row
XSSFRow row=targetSheet.createRow(i+num1-startRow+1);
//cell
XSSFCell cell=row.createCell(j); //j 行
二、 操作單元格函數
POI能夠讀取函數,然後再把函數寫入到單元格中,excel自己計算函數。而函數操作單元格的位置,一般是固定的,所以操作的單元格無法改變。
1、讀取函數和寫入函數
cell.getCellFormula()