java导出数据并保存为excel文件。
1.下载poi.jar 和 poi-ooxml.jar 需要使用这两个包。放到lib下文件夹中
下载地址直接百度就可以 做好去官网下载最新版本。
2 住要方法体
public static String generateExcel(String titly,List<Police> data,String path,String fileName){
//得到笔记本对象
HSSFWorkbook workbook = new HSSFWorkbook();
// 建议使用 XSSFWorkbook
// Workbook workbook = new XSSFWorkbook();
//创建 纸张
Sheet sheet = workbook.createSheet();
//隐藏 指定的 列 没有适应
//sheet.setColumnHidden(0, 10 * 256);
//设置指点单元格的宽度 这是第7个单元格 “情况”加宽了。
sheet.setColumnWidth(6, 20 * 256);
//合并单元格 这合并的是大标题行
sheet.addMergedRegion(new CellRangeAddress(
0, //first row (0-based) 开始行号
0, //last row (0-based) 结束行号
0, //first column (0-based)开始单元格
10//last column (0-based) 结束单元格
));
//创建第一行 大标题行
Row row = sheet.createRow(0);
//设置第一行的行高
row.setHeightInPoints(40);
//创建第一行的第一个单元格
Cell cell = row.createCell(0);
//创建第一行其他单体格
for (int i = 0; i < 11; i++) {
row.createCell(i);
}
//设置头样式 方法体在下面3.
HSSFCellStyle columnstyle = columnstyle(workbook);
cell.setCellStyle(columnstyle);
//添加表头信息 方法体下面3.
HSSFCellStyle style = cellstyle(workbook);
cell.setCellValue(titly);
//第二行 小标题行 就是列的名称
Row row2 = sheet.createRow(1);
row2.createCell(0).setCellValue("序号");
row2.createCell(1).setCellValue("单位");
row2.createCell(2).setCellValue("地址");
row2.createCell(3).setCellValue("姓名");
row2.createCell(4).setCellValue("性别");
row2.createCell(5).setCellValue("记录时间");
row2.createCell(6).setCellValue("情况");
row2.createCell(7).setCellValue("上班时间");
row2.createCell(8).setCellValue("下班原因");
row2.createCell(9).setCellValue("项目");
row2.createCell(10).setCellValue("备注");
for (int i = 0; i < data.size(); i++) {
Row rr = sheet.createRow(i+2);
for (int j = 0; j < 11; j++) {
//设置列名称样式
row2.getCell(j).setCellStyle(style);
//设置行高 这个是列名称的那行
row2.setHeightInPoints(30);
//设置行高 其余的行
rr.setHeightInPoints(30);
Cell cc = rr.createCell(j);
cc.setCellValue(j+"zhi");
//设置 文本样式
cc.setCellStyle(style);
}
}
//指定路径的存放
File file = new File(path);
if(!file.exists()){
file.mkdirs();
file = new File(path+"\\"+fileName);
}else{
file = new File(path+"\\"+fileName);
}
try {
OutputStream ops = new FileOutputStream(file);
workbook.write(ops);
ops.flush();
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "失败";
}
return "成功";
}
3.设置样式
//设置表头格式
public static HSSFCellStyle columnstyle(HSSFWorkbook wb){
//字体格式
HSSFFont font = wb.createFont();
//设置字体大小
font.setFontHeightInPoints((short)20);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置样式
HSSFCellStyle style = wb.createCellStyle();
//左右居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置底部边框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底部颜色
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边颜色
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边颜色
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶部的边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶部颜色
style.setTopBorderColor(HSSFColor.BLACK.index);
//自动换行
style.setWrapText(false);
//设置字体风格
style.setFont(font);
return style;
}
//设置正文样式
public static HSSFCellStyle cellstyle(HSSFWorkbook wb){
//字体格式
HSSFFont font = wb.createFont();
//设置字体大小
font.setFontHeightInPoints((short)12);
//字体加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置样式
HSSFCellStyle style = wb.createCellStyle();
//左右居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置底部边框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底部颜色
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边颜色
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边颜色
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶部的边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶部颜色
style.setTopBorderColor(HSSFColor.BLACK.index);
//自动换行
style.setWrapText(false);
//字体风格
style.setFont(font);
return style;
}
4.main方法
public static void main(String[] args) {
//创建参数类型 这里使用list
List<Police> data = new ArrayList<Police>();
// 绑定测试参数的值
for (int i = 0; i < 10; i++) {
Police police = new Police();
police.setId("100"+i);
police.setName(String.valueOf(i));
police.setPassword("1212"+i);
data.add(police);
System.out.println(police);
}
// 标题
String titly = "执法办案场所管理使用台账";
// 下载到本地路径
String path = "D:\\test";
// 文件名称
String fileName = "new.xls";
// 执行方法
String res = generateExcel(titly, data, path, fileName);
// 打印结果
System.out.println(res);
}
完成后的贴图