一、我常用的几个对象
HSSFWorkbook
1、含义:excel的工作簿
2、创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
3、创建 excel 的工作表
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
4、创建单元格样式
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFSheet
1、含义:excel 的工作表
2、创建行(第一行从 0 开始)
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
3、设置单元格宽度
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
sheet.setColumnWidth(0, 8*256)
HSSFCellStyle
1、含义:单元格样式
2、属性设置
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setBorderBottom(BorderStyle.MEDIUM);
cellStyle.setBorderLeft(BorderStyle.MEDIUM);
cellStyle.setBorderRight(BorderStyle.MEDIUM);
cellStyle.setBorderTop(BorderStyle.MEDIUM);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 22);
font.setBold(true);
cellStyle.setFont(font);
HSSFRow
1、含义:excel 的行
2、创建行对应的单元格(第一个单元格从 0 开始)
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
3、属性设置
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
row.setHeightInPoints(30f);
HSSFCell
1、excel 的单元格
2、属性设置
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellStyle(cellStyle);
cell.setCellValue(cellValue);
CellRangeAddress
1、含义:单元格范围地址(合并单元格需使用)
2、单元格范围地址创建(先设置范围再设置单元格不容易弄乱)
CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 14);
sheet.addMergedRegion(region);
RegionUtil.setBorderBottom(BorderStyle.MEDIUM, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.MEDIUM, region, sheet);
RegionUtil.setBorderRight(BorderStyle.MEDIUM, region, sheet);
RegionUtil.setBorderTop(BorderStyle.MEDIUM, region, sheet);
二、导出 Excel 代码
1、导出到指定文件里面
public void exportExcel() {
OutputStream out = null;
try {
out = new FileOutputStream("D:/test.xlsx");
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
if (out != null) {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
2、浏览器导出
public void exportExcel(HttpServletResponse response) throws IOException, DocumentException {
OutputStream outputStream = response.getOutputStream();
response.reset();
String filename = "Excel文件名称.xlsx";
response.setContentType("application/msexcel");
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
3、如果代码太多,可以根据自己需求把代码部分代码提取出来
private HSSFCellStyle createCellStyle(HSSFWorkbook workbook, HorizontalAlignment align, boolean isBorder, boolean isForeground, int fontHeightInPoints, boolean isBold) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(align);
if (isBorder) {
cellStyle.setBorderBottom(BorderStyle.MEDIUM);
cellStyle.setBorderLeft(BorderStyle.MEDIUM);
cellStyle.setBorderRight(BorderStyle.MEDIUM);
cellStyle.setBorderTop(BorderStyle.MEDIUM);
}
if (isForeground) {
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
}
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) fontHeightInPoints);
if (isBold) {
font.setBold(true);
}
cellStyle.setFont(font);
return cellStyle;
}
private void createCellRangeToSheetAndInsertCell(int firstRow, int lastRow, int firstCol, int lastCol, Sheet sheet, HSSFRow row, int cellNum, String cellValue, HSSFCellStyle cellStyle, boolean isBorder) {
CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
sheet.addMergedRegion(region);
if (isBorder) {
RegionUtil.setBorderBottom(BorderStyle.MEDIUM, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.MEDIUM, region, sheet);
RegionUtil.setBorderRight(BorderStyle.MEDIUM, region, sheet);
RegionUtil.setBorderTop(BorderStyle.MEDIUM, region, sheet);
}
HSSFCell cell = row.createCell(cellNum);
cell.setCellValue(cellValue);
cell.setCellStyle(cellStyle);
}