二、下面就简单的解释一下这几个文件
1. ExcelGenerate .java文件 这个文件中每个方法都有解释,好好的看看注释
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
/**
*
* @author 岑
*
*/
public class ExcelGenerate {
public static HSSFWorkbook wb;
private static HSSFSheet sheet;
private static HSSFRow newRow;
private static HSSFCellStyle cellSytle;
private static HSSFCell newCell;
ExcelGenerate() {
wb = new HSSFWorkbook();
}
public HSSFWorkbook getInstance() {
if (wb == null) {
wb = new HSSFWorkbook();
}
return wb;
}
/**
* sheet 取得
* @param sheet 对象 HSSFSheet
*/
public static HSSFSheet getSheet(String sheetName) {
sheet = wb.createSheet(sheetName);
return sheet;
}
/**
* 新建行
*
* @param sheet HSSFSheet 对象
* @param rownum 行号 0:第一行 1:第2行...
* @return row HSSFRow 对象
*/
public static HSSFRow getNewRow(HSSFSheet sheet, int rownum) {
newRow = sheet.createRow(rownum);
return newRow;
}
public static HSSFCell getNewCell(HSSFRow row, int cellnum) {
newCell = row.createCell(cellnum);
return newCell;
}
/**
* 合并单元格
*
* @param firstRow 开始行
* @param lastRow 结束行
* @param firstCol 开始列
* @param lastCol 结束列
*/
public static void CellMergedRegion(int firstRow, int lastRow, int firstCol, int lastCol) {
CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol,lastCol);
sheet.addMergedRegion(region);
}
/**
* 样式表
*
* @return CellStyle HSSFCellStyle
*/
public static HSSFCellStyle getCellStyle() {
// 创建单元格样式
cellSytle = wb.createCellStyle();
return cellSytle;
}
/**
* 设置单元格边框
*
* @param colour 边框颜色 例如: HSSFColor.RED.index
* @param borderBottom 边框底部 例如:HSSFCellStyle.BORDER_THIN
* @param borderLeft 边框左边 例如:HSSFCellStyle.BORDER_THIN
* @param borderRight 边框右边 例如:HSSFCellStyle.BORDER_THIN
* @param borderTop 边框顶部 例如:HSSFCellStyle.BORDER_THIN
* @see org.apache.poi.hssf.util.HSSFColor;
* @see org.apache.poi.hssf.usermodel.HSSFCellStyle
*
*/
public static void setBorder(HSSFCellStyle sytle,short colour, short borderBottom, short borderLeft, short borderRight, short borderTop) {
// 设置单元格边框
sytle.setBottomBorderColor(colour);
sytle.setBorderBottom(borderBottom);
sytle.setBorderLeft(borderLeft);
sytle.setBorderRight(borderRight);
sytle.setBorderTop(borderTop);
}
/**
* 设置单元格颜色
*
* @param colour 颜色设定 例如: HSSFColor.RED.index
* @param fillPattern 填充颜色 例如:HHSSFCellStyle.SOLID_FOREGROUND
* @see org.apache.poi.hssf.util.HSSFColor;
* @see org.apache.poi.hssf.usermodel.HSSFCellStyle
*/
public static void setForegroundColor(HSSFCellStyle sytle,int colour, int fillPattern) {
// 设定单元格背景色
sytle.setFillForegroundColor(HSSFColor.WHITE.index);
sytle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
}
/**
* 画表格
*
* @param startRow 开始行
* @param endRow 结束行
* @param startCell 开始格
* @param endCell 结束格
* @param cellSytle 表格样式
*/
public static void setChart(int startRow, int endRow, int startCell, int endCell, HSSFCellStyle cellSytle){
for (int i = startRow; i <= endRow; i++) {
newRow = sheet.createRow(i);
for (int j = startCell; j <= endCell; j++) {
newCell = newRow.createCell(j);
newCell.setCellStyle(cellSytle);
}
}
}
/**
*
* @param wb 工作表
* @param filePath 文件名和路径
* @throws IOException
* @throws E