主要的思想就是:先设置路径以及导出excel表格名字,要有一个模板excel(导出的excel表格是以这个为模板的,就是模板上基本的名字合并单元格都已经弄好了,我们只要进行插值就好了),然后通过一个类方法在模板上插值。
需要三个包:poi-3.0.2-FINAL-20080204.jar
poi-contrib-3.0.2-FINAL-20080204.jar
poi-scratchpad-3.0.2-FINAL-20080204.jar
excel类(最主要的功能类)
需要三个包:poi-3.0.2-FINAL-20080204.jar
poi-contrib-3.0.2-FINAL-20080204.jar
poi-scratchpad-3.0.2-FINAL-20080204.jar
excel类(最主要的功能类)
public void exportExcel(String s , int x , int y , OutputStream out) throws FileNotFoundException, IOException{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("model/xcbg.xls"));
//这里是设置需要参考的模板excel,特别重要!一切的填写都是按照这个模板填写的,最后生成的excel也是按照这个模板生成的
//只不过比起模板,我们生成的新excel会加上我们自己的信息
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row = sheet.getRow(x-1);
// HSSFCell cell = row.getCell((short)0);
HSSFCell cell = row.createCell((short)(y-1));
cell.setCellValue(s);
HSSFCellStyle cellStyle = cell.getCellStyle();
try{
workbook.write(out);//客户端下载该excel
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//这个方法是单独对一块没有规律的excel填写用的,s是填写的信息 ,例如 :你好;x,y分别表示第几行第几列(不是从0开始);out表示写入哪个文件里去。
//
public void exportExcelMain (int xStart , Collection<T> dataset ,
OutputStream out) throws FileNotFoundException, IOException{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("model/xcbg.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFDataFormat format = workbook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("@"));
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.set