您的位置:首页 > 编程语言 > Java开发

java 生成Excel

2017-04-05 15:02 417 查看
首先网上关于代码创建excel大致分为两种:

1.采用poi;

2.采用jxl;

(菜鸟个人观点,当然也有很多现成的工具类等等)

我所采用的是poi的方式:

首先创建Workbook(poi提供了两种):HSSFWorkbook、XSSFWorkbook

注:HSSFWorkbook 是后缀为xls(2003-2007)版本

XSSFWorkbook 为2007以上版本

HSSFWorkbook  xls = new  HSSFWorkbook();//创建Workbook
HSSFSheet sheet = xls.createSheet("newsheet");//创建sheet
HSSFCellStyle style = xls.createCellStyle();//设置单元格格式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中


接下来创建sheet页内容

1、Title

HSSFRow row = sheet.createRow((int) 0);
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
HSSFCell cell = row.createCell(0);
cell.setCellValue("区域");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("商品名称");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("当日兑换量");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("成本价");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("结算单价");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("销售价");
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("当日兑换金额");
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("当日收入");
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("累计兑换量");
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("累计收入");
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("累计兑换金额");


注:sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));

合并单元格



(注意包的路径:org.apache.poi.ss.util.CellRangeAddress,有的为org.apache.poi.hssf.util.CellRangeAddress已废弃)



方法的参数(index坐标从0开始)

firstRow :起始行;

lastRow:结束行;

firstCol:起始单元格;

lastCol:结束单元格;

2、单元格内容

HSSFRow row = sheet.createRow(1);
sheet.addMergedRegion(new CellRangeAddress(1, 3, 0, 0));
HSSFCell cell = row.createCell(0);
cell.setCellValue("江苏");
cell.setCellStyle(style);
cell = row.createCell(1);
sheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 1));
4000
cell.setCellValue("移动");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("aaa");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("8");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("20.00");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("32.98");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("40.00");
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("320.00");
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("263.84");
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("1110");
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("36607.80");
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("44400.00");
cell.setCellStyle(style);
row = sheet.createRow(2);
cell = row.createCell(2);
cell.setCellValue("aaa");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("8");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("20.00");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("32.98");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("40.00");
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("320.00");
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("263.84");
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("1110");
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("36607.80");
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("44400.00");
cell.setCellStyle(style);
row = sheet.createRow(3);
sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, 1));
cell = row.createCell(1);
cell.setCellValue("电信");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("aaa");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("8");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("20.00");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("32.98");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("40.00");
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("320.00");
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("263.84");
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("1110");
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("36607.80");
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("44400.00");
cell.setCellStyle(style);
row = sheet.createRow(4);
sheet.addMergedRegion(new CellRangeAddress(4, rowIndex, 0, 2));
cell = row.createCell(0);
cell.setCellValue("江苏合计");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("2");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("-");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("-");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("-");
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("320");
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("263.84");
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("111");
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("3688");
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("4200");
cell.setCellStyle(style);




到这里 一个excel就已用java创建完毕

接着需要把生成的Excel输出

String webParentPath = new File(request.getSession().getServletContext().getRealPath("/")).getParent();// 当前WEB环境的上层目录
String realPath = webParentPath + "/file/";// 文件上传到服务器的真实路径
File f = new File(realPath);
try{
if (!f.isDirectory()) {
f.mkdirs();
}

FileOutputStream xlsxfileOut = new FileOutputStream(realPath+"123.xls");
xls.write(xlsxfileOut);
xlsxfileOut.close();


以上为demo关键代码;小菜献上(注代码逻辑还需重新排列)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息