如何用Java的POI库创建Excel文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计778个文字,预计阅读时间需要4分钟。
使用POI生成Excel,包含以下步骤:创建一个工作簿、创建一个工作表、创建一个行对象、创建一个单元格对象(由一个行对象和一个单元格对象组成一个单元格格式)。设置单元格内容、设置单元格格式。字体、字体大小是。
使用poi生成excel通常包含一下几个步骤- 创建一个工作簿
- 创建一个sheet
- 创建一个Row对象
- 创建一个cell对象(1个row+1个cell构成一个单元格)
- 设置单元格内容
- 设置单元格样式. 字体 字体大小 是否加粗
- 保存
- 关闭流对象
2010以上格式使用XSSFWorkBook对象, 2003格式使用HSSFWorkBook对象, 其他对象操作基本一样.
public void test1() {
HSSFWorkbook workbook = new HSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
HSSFSheet sheet = workbook.createSheet("Sheet1");
//设置单元格宽度
sheet.setColumnWidth(0, 30 * 256);
sheet.setColumnWidth(1, 30 * 256);
sheet.setColumnWidth(2, 30 * 256);
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
cell0.setCellValue("序号");
cell0.setCellStyle(cellStyle);
Cell cell1 = row0.createCell(1);
cell1.setCellValue("姓名");
Cell cell2 = row0.createCell(2);
cell2.setCellValue("成绩");
OutputStream os = null;
try {
os = new FileOutputStream("d:\\测试生成2003.xls");
workbook.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
生成2010以上格式
@Test
public void test2() {
XSSFWorkbook workbook = new XSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
XSSFSheet sheet = workbook.createSheet("Sheet1");
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
cell0.setCellValue("序号");
cell0.setCellStyle(cellStyle);
Cell cell1 = row0.createCell(1);
cell1.setCellValue("姓名");
Cell cell2 = row0.createCell(2);
cell2.setCellValue("成绩");
OutputStream os = null;
try {
os = new FileOutputStream("d:\\测试生成2010.xlsx");
workbook.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
合并单元格
合并单元格在生成excel中算常见的一个场景, 通常先合并单元, 单元格内容居中,并设置单元格边框.
poi合并单元格使用CellRangeAddress类, 构造函数包括4个参数firstRow, lastRow, firstCol, lastCol根据自己需要传入行和列.
public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
}
合并单元格后设置边框poi已提供了RegionUtil静态类, 可直接使用.
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(region);
RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
设置单元格样式
左右居中 上下居中 自动换行
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
使用SpringMVC/SpringBoot导出excel
@Controller
@GetMapping("/excel2003")
public void excel2003(HttpServletResponse www.cnblogs.com/sword-successful/
博客版权:本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。
如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如一下!如果你有更好的建议,不如留言一起讨论,共同进步!
再次感谢您耐心的读完本篇文章。
本文共计778个文字,预计阅读时间需要4分钟。
使用POI生成Excel,包含以下步骤:创建一个工作簿、创建一个工作表、创建一个行对象、创建一个单元格对象(由一个行对象和一个单元格对象组成一个单元格格式)。设置单元格内容、设置单元格格式。字体、字体大小是。
使用poi生成excel通常包含一下几个步骤- 创建一个工作簿
- 创建一个sheet
- 创建一个Row对象
- 创建一个cell对象(1个row+1个cell构成一个单元格)
- 设置单元格内容
- 设置单元格样式. 字体 字体大小 是否加粗
- 保存
- 关闭流对象
2010以上格式使用XSSFWorkBook对象, 2003格式使用HSSFWorkBook对象, 其他对象操作基本一样.
public void test1() {
HSSFWorkbook workbook = new HSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
HSSFSheet sheet = workbook.createSheet("Sheet1");
//设置单元格宽度
sheet.setColumnWidth(0, 30 * 256);
sheet.setColumnWidth(1, 30 * 256);
sheet.setColumnWidth(2, 30 * 256);
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
cell0.setCellValue("序号");
cell0.setCellStyle(cellStyle);
Cell cell1 = row0.createCell(1);
cell1.setCellValue("姓名");
Cell cell2 = row0.createCell(2);
cell2.setCellValue("成绩");
OutputStream os = null;
try {
os = new FileOutputStream("d:\\测试生成2003.xls");
workbook.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
生成2010以上格式
@Test
public void test2() {
XSSFWorkbook workbook = new XSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
XSSFSheet sheet = workbook.createSheet("Sheet1");
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
cell0.setCellValue("序号");
cell0.setCellStyle(cellStyle);
Cell cell1 = row0.createCell(1);
cell1.setCellValue("姓名");
Cell cell2 = row0.createCell(2);
cell2.setCellValue("成绩");
OutputStream os = null;
try {
os = new FileOutputStream("d:\\测试生成2010.xlsx");
workbook.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
合并单元格
合并单元格在生成excel中算常见的一个场景, 通常先合并单元, 单元格内容居中,并设置单元格边框.
poi合并单元格使用CellRangeAddress类, 构造函数包括4个参数firstRow, lastRow, firstCol, lastCol根据自己需要传入行和列.
public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
}
合并单元格后设置边框poi已提供了RegionUtil静态类, 可直接使用.
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(region);
RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
设置单元格样式
左右居中 上下居中 自动换行
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
使用SpringMVC/SpringBoot导出excel
@Controller
@GetMapping("/excel2003")
public void excel2003(HttpServletResponse www.cnblogs.com/sword-successful/
博客版权:本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。
如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如一下!如果你有更好的建议,不如留言一起讨论,共同进步!
再次感谢您耐心的读完本篇文章。

