标签:title 结束 bool 导出 date() ati 居中 code time()
【前言】 //声明工作薄
HSSFWorkbook wb = new HSSFWorkbook();
//sheet页签部分
HSSFSheet sheet = wb.createSheet("页签的名字");
//合并标题
sheet.addMergedRegion(new Region(0, (short)0, (short)0,(short)(规划的列数)));
//创建表头
row = sheet.createRow(1);
//第一层为循环创建行
for (int i = 0; i < contentLst.size(); i++) {
row = sheet.createRow(i+2);
row.setHeight((short) 550);
//第二层创建每行的单元格,并填内容
for (int j = 0; j < contentLst.get(i).length; j++) {
cell = row.createCell(j);
cell.setCellValue(String.valueOf(contentLst.get(i)[j]));
cell.setCellStyle(shstyle);
}
}
//创建标题
HSSFRow row = sheet.createRow(0);
//设置标题行高
row.setHeight((short) 行高数);
//自定义列宽部分,你将每个列宽作为参数传过来具体每列多宽得自己测试了。
if(liekuanLst != null && liekuanLst.size() > 0){
for (int i = 0; i < liekuanLst.size(); i++) {
sheet.setColumnWidth((short)i, liekuanLst.get(i));
}
}
//居中字体等样式区域
sheet.setHorizontallyCenter(true);
//主题
HSSFCellStyle titlefontshstyle = wb.createCellStyle();
HSSFFont titlefont = wb.createFont();
setcontentStyleTable(titlefontshstyle, titlefont, "黑体", 20, true, false);
public static void setcontentStyleTable(HSSFCellStyle shstyle, HSSFFont titlefont, String string, int i, Boolean is_bold, Boolean is_border) {
titlefont.setFontHeightInPoints((short) i);
titlefont.setFontName(string);
shstyle.setWrapText(true);//自动换行
shstyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
if(i != 11 && is_bold){
titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
}
if(is_border){
shstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
shstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左
shstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右
shstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上
}
shstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
shstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//定义字体样式左右居中
shstyle.setFont(titlefont);
}
//添加打印样式
addPrintClassData(sheet, false);
//打印的纸张样式
public static void addPrintClassData(HSSFSheet sheet, Boolean is_landscape) {
sheet.setMargin(HSSFSheet.TopMargin, 0.4);//上≈2
sheet.setMargin(HSSFSheet.BottomMargin, 0.4);//下≈2
sheet.setMargin(HSSFSheet.LeftMargin, 0.2);//左≈0.5
sheet.setMargin(HSSFSheet.RightMargin, 0.2);//右≈0.5
sheet.setHorizontallyCenter(true);
sheet.setDefaultRowHeight((short) 400);//设置默认行高
HSSFPrintSetup ps = sheet.getPrintSetup();
ps.setLandscape(is_landscape);//true横向,false纵向
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//设置纸张A4
}
ByteArrayOutputStream bos = null;
try{
/*1转为输入流*/
bos = new ByteArrayOutputStream();
wb.write(bos);
byte[] bytes = bos.toByteArray();
InputStream in = new ByteArrayInputStream(bytes);
/*2直接写入Excel文档*/
/*String fileName = new Date().getTime()+"frozen_excel.xls";
FileOutputStream output = new FileOutputStream("/temp/"+fileName);
wb.write(output);
output.close();*/
bos.close();
}catch (Exception e){
e.printStackTrace();
}
【总结】
看看上面9步需要哪些参数,将参数封装做一个统一的公共方法,也就是
/**
* 导出Excel
* @param title Excel标题
* @param liekuanLst 设置的每个列宽
* @param biaotouLst 表头内容
* @param contentLst 单元格内容
* @return 待定
*/
public String export_Excel(String title, List<Integer> liekuanLst, List<String> biaotouLst, List<Object[]> contentLst){
//声明工作薄
HSSFWorkbook wb = new HSSFWorkbook();
//sheet页签部分
HSSFSheet sheet = wb.createSheet(title);
//自定义列宽部分
if(liekuanLst != null && liekuanLst.size() > 0){
for (int i = 0; i < liekuanLst.size(); i++) {
sheet.setColumnWidth((short)i, liekuanLst.get(i));
}
}
//居中字体等样式区域
sheet.setHorizontallyCenter(true);
//主题
HSSFCellStyle titlefontshstyle = wb.createCellStyle();
HSSFFont titlefont = wb.createFont();
setcontentStyleTable(titlefontshstyle, titlefont, "黑体", 20, true, false);
//表头样式
HSSFCellStyle btfontshstyle = wb.createCellStyle();
HSSFFont btfont = wb.createFont();
setcontentStyleTable(btfontshstyle, btfont, "宋体", 10, true, true);
//内容样式
HSSFCellStyle shstyle = wb.createCellStyle();
HSSFFont contentfont = wb.createFont();
setcontentStyle(shstyle, contentfont, "宋体", 11);
//创建标题
HSSFRow row = sheet.createRow(0);
//设置标题行高
row.setHeight((short) 920);
HSSFCell cell = row.createCell(0);
cell.setCellValue(title);
cell.setCellStyle(titlefontshstyle);
for (int i = 1; i < liekuanLst.size(); i++) {
cell = row.createCell(i);
cell.setCellStyle(titlefontshstyle);
}
//合并标题
sheet.addMergedRegion(new Region(0, (short)0, (short)0,(short)(liekuanLst.size()-1)));
//创建表头
row = sheet.createRow(1);
//行高
row.setHeight((short) 550);
if(biaotouLst != null && biaotouLst.size() > 0){
for (int i = 0; i < biaotouLst.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(biaotouLst.get(i));
cell.setCellStyle(btfontshstyle);
}
}
if(contentLst != null && contentLst.size() > 0){
//循环插入表格内容
for (int i = 0; i < contentLst.size(); i++) {
//当前行
row = sheet.createRow(i+2);
row.setHeight((short) 550);
//每个单元格
for (int j = 0; j < contentLst.get(i).length; j++) {
cell = row.createCell(j);
cell.setCellValue(String.valueOf(contentLst.get(i)[j]));
cell.setCellStyle(shstyle);
}
}
}
//添加打印样式
addPrintClassData(sheet, false);
/**
* 写入数据
*/
ByteArrayOutputStream bos = null;
try{
/*1转为输入流*/
bos = new ByteArrayOutputStream();
wb.write(bos);
byte[] bytes = bos.toByteArray();
InputStream in = new ByteArrayInputStream(bytes);
/*2直接写入Excel文档*/
/*String fileName = new Date().getTime()+"frozen_excel.xls";
FileOutputStream output = new FileOutputStream("/temp/"+fileName);
wb.write(output);
output.close();*/
bos.close();
}catch (Exception e){
e.printStackTrace();
}
return "";
}
//单元格样式
public static void setcontentStyle(HSSFCellStyle shstyle, HSSFFont titlefont, String string, int i) {
titlefont.setFontHeightInPoints((short) i);
titlefont.setFontName(string);
shstyle.setWrapText(true);//自动换行
if(i != 11){
titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
}
shstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//定义字体样式左右居中
shstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
shstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
shstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左
shstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右
shstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上
shstyle.setFont(titlefont);
}
public static void setcontentStyleTable(HSSFCellStyle shstyle, HSSFFont titlefont, String string, int i, Boolean is_bold, Boolean is_border) {
titlefont.setFontHeightInPoints((short) i);
titlefont.setFontName(string);
shstyle.setWrapText(true);//自动换行
shstyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
if(i != 11 && is_bold){
titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
}
if(is_border){
shstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
shstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左
shstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右
shstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上
}
shstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
shstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//定义字体样式左右居中
shstyle.setFont(titlefont);
}
//打印的纸张样式
public static void addPrintClassData(HSSFSheet sheet, Boolean is_landscape) {
sheet.setMargin(HSSFSheet.TopMargin, 0.4);//上≈2
sheet.setMargin(HSSFSheet.BottomMargin, 0.4);//下≈2
sheet.setMargin(HSSFSheet.LeftMargin, 0.2);//左≈0.5
sheet.setMargin(HSSFSheet.RightMargin, 0.2);//右≈0.5
sheet.setHorizontallyCenter(true);
sheet.setDefaultRowHeight((short) 400);//设置默认行高
HSSFPrintSetup ps = sheet.getPrintSetup();
ps.setLandscape(is_landscape);//true横向,false纵向
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//设置纸张A4
}
【结束语】
还可以继续优化!
标签:title 结束 bool 导出 date() ati 居中 code time()
原文地址:https://blog.51cto.com/13479739/2472555