java 基于 POI 导出 excel文件

来源:互联网 发布:百视通机顶盒下载软件 编辑:程序博客网 时间:2024/05/10 10:55

生成 excel 的方法有两种。第一种,就像修房子一样,一块砖一块瓦,一点点修葺起来; 第二种,先导入个模版,再将自己要显示的数据放在 需要显示 的地方,然后生成excel. 两种方法各有利弊,相信对大家而言是不言而喻的,使用时自己做权衡取舍。

第一种,原生态 法:(PoiCreateExcel.java)

<span style="font-size:18px;">package com.conserv.tsas.test;import java.io.File;import java.io.FileOutputStream;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFDataFormat;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFHyperlink;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 Administrator * 无模版 生成 excel  */public class PoiCreateExcel {public static void main(String[] args) throws Exception {// 创建 Excel 的工作书册 Workbook, 对应到一个 excel 文档HSSFWorkbook wb = new HSSFWorkbook();// 创建 Excel 的工作 sheet, 对应到一个excel文档的tabHSSFSheet sheet = wb.createSheet("sheet_1");// 设置 excel 每列的宽度sheet.setColumnWidth(0, 4000);sheet.setColumnWidth(1, 4000);//sheet.setColumnWidth(1, 8000);// 创建字体样式HSSFFont font = wb.createFont();font.setFontName("宋体");font.setBoldweight((short) 100);font.setFontHeight((short) 300);font.setColor(HSSFColor.BLUE.index);// 创建单元格样式HSSFCellStyle style = wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_CENTER);style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 设置边框style.setBottomBorderColor(HSSFColor.RED.index);style.setBorderTop(HSSFCellStyle.BORDER_THIN);style.setBorderBottom(HSSFCellStyle.BORDER_THIN);style.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 设置字体style.setFont(font);// 创建  excel 的 sheet 的一行HSSFRow row = sheet.createRow(0);row.setHeight((short) 500); // 设置行高// 创建一个 excel 的单元格HSSFCell cell = row.createCell(0);HSSFCell cell2 = row.createCell(1);cell2.setCellStyle(style);// 合并单元格sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));// 给 excel  设置样式 和赋值cell.setCellStyle(style);cell.setCellValue(" hell word ");// 设置单元格 内容格式HSSFCellStyle style1 = wb.createCellStyle();style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 自动换行style1.setWrapText(true);style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);//style1.setBorderTop(HSSFCellStyle.BORDER_THIN);style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);style1.setBorderRight(HSSFCellStyle.BORDER_THIN); row = sheet.createRow(1);row.setHeight((short) 500);// 设置单元格样式cell = row.createCell(0);cell.setCellStyle(style1);cell.setCellValue(new Date());HSSFCellStyle style2 = wb.createCellStyle();style2.setAlignment(HSSFCellStyle.ALIGN_RIGHT);style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);//style2.setBorderTop(HSSFCellStyle.BORDER_THIN);style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);style2.setBorderRight(HSSFCellStyle.BORDER_THIN); // 创建超级链接HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);link.setAddress("http://www.baidu.com");cell = row.createCell(1);cell.setCellValue("百度");// 设置单元格链接cell.setHyperlink(link);cell.setCellStyle(style2);FileOutputStream fos = new FileOutputStream(new File("d:/workbook.xls"));wb.write(fos);fos.close();System.out.println("success!");}}

最终效果:




第二种,模版 法:

package com.conserv.tsas.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;public class PoiTestExcel {public static void main(String[] args) {try {File file = new File("d:/model.xls");// InputStream is = new FileInputStream("");POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));Workbook work = new HSSFWorkbook(fs);// 得到 excel 的第 0 张表Sheet sheet = work.getSheetAt(0);// 得到第 三 行的第一个单元格的样式Row rowcellStyle = sheet.getRow(2);CellStyle column1 = rowcellStyle.getCell(0).getCellStyle();// 这里的行和列的数法与计算机里的一样,从 0 开始计数// 填充 title 数据Row row = sheet.getRow(0);Cell cell = row.getCell(0);cell.setCellValue("复旦大学    2010年花样名册");int number = 0, i = 0;// 得到行,并填充数据和表格样式for (i = 2; i < 12; i++) {//row = sheet.getRow(i); // 得到行row = sheet.createRow(i);row.setHeight((short) 400);cell = row.createCell(0);// 得到第 0 个单元格cell.setCellValue("貂蝉 " + (i-1));cell.setCellStyle(column1); // 填充样式cell = row.createCell(1);cell.setCellValue("女");cell.setCellStyle(column1);// 填充样式cell = row.createCell(2);Date date = new Date();SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");cell.setCellValue(df.format(date));cell.setCellStyle(column1);// 填充样式cell = row.createCell(3);cell.setCellValue("15901215678");cell.setCellStyle(column1);// 填充样式cell = row.createCell(4);cell.setCellValue("上海市浦东世纪大道");cell.setCellStyle(column1);// 填充样式cell = row.createCell(5);cell.setCellValue("无");cell.setCellStyle(column1);// 填充样式// .....给每个单元格填充数据和样式number++;}// 最后一行, 创建每个单元格,添加样式,然后合并row = sheet.createRow(i);row.setHeight((short) 500);cell = row.createCell(0);cell.setCellValue("总计:" + number + "个学生"); // 填充值cell.setCellStyle(column1);cell = row.createCell(1);cell.setCellStyle(column1);cell = row.createCell(2);cell.setCellStyle(column1);cell = row.createCell(3);cell.setCellStyle(column1);cell = row.createCell(4);cell.setCellStyle(column1);cell = row.createCell(5);cell.setCellStyle(column1);// 合并单元格sheet.addMergedRegion(new CellRangeAddress(i, i, 0, 5));FileOutputStream fos = new FileOutputStream("d:/workbook_2.xls");work.write(fos);fos.close();System.out.println("恭喜你,  操作成功!");} catch (FileNotFoundException e) {System.out.println("文件路径错误!");e.printStackTrace();} catch (IOException e) {System.out.println("文件输入流错误!");e.printStackTrace();}}}
导入的模版:



操作成功后的 excel 效果:



呵呵, 是不是又快又漂亮呢??? 

如果,本文对你有用,亲,记得好评哟!!!



0 0