Java实现POI导出Excel
Posted skiwnchqhh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现POI导出Excel相关的知识,希望对你有一定的参考价值。
Web框架为Struts2,所用jar包如下:
ExportExcelAction.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.Region;
public class ExportExcelAction {
private InputStream inputStream;
public String export() {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
sheet.setDefaultColumnWidth(20); // 默认列宽
HSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 13);// 设置字体大小
font.setColor(HSSFColor.WHITE.index); //字体颜色
HSSFCellStyle headStyle = wb.createCellStyle(); // 头部样式
headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
headStyle.setFillForegroundColor(HSSFColor.TEAL.index);// 设置背景色
headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headStyle.setFont(font);// 选择需要用到的字体格式
HSSFCellStyle contentStyle = wb.createCellStyle(); // 内容样式
contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
HSSFRow row_h = sheet.createRow((short) 0);
HSSFCell ch = row_h.createCell(0);
ch.setCellValue("测试导出Excel");
ch.setCellStyle(headStyle);
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 2));// 指定合并区域
//表头
HSSFRow row1 = sheet.createRow((short) 1);
HSSFCell cell_1_0 = row1.createCell(0);
cell_1_0.setCellValue("姓名");
cell_1_0.setCellStyle(headStyle);
HSSFCell cell_1_1 = row1.createCell(1);
cell_1_1.setCellValue("出生日期");
cell_1_1.setCellStyle(headStyle);
HSSFCell cell_1_2 = row1.createCell(2);
cell_1_2.setCellValue("住址");
cell_1_2.setCellStyle(headStyle);
//内容
HSSFRow row2 = sheet.createRow(2);
HSSFCell cell_2_0 = row2.createCell(0);
cell_2_0.setCellValue("itmyhome");
cell_2_0.setCellStyle(contentStyle);
HSSFCell cell_2_1 = row2.createCell(1);
cell_2_1.setCellValue("1990-05-01");
cell_2_1.setCellStyle(contentStyle);
HSSFCell cell_2_2 = row2.createCell(2);
cell_2_2.setCellValue("北京市昌平区");
cell_2_2.setCellStyle(contentStyle);
FileOutputStream fileOut = new FileOutputStream("d:/mysheet.xls"); //导出路径
wb.write(fileOut);
fileOut.close();
inputStream = new FileInputStream("d:/mysheet.xls"); //下载
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="json_code" extends="json-default">
<!-- 导出报表 -->
<action name="exportExcelAction" method="export"
class="com.itmyhome.ExportExcelAction">
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="export.xls"</param>
<param name="bufferSize">4096</param>
</result>
</action>
</package>
<package name="build" extends="struts-default">
<action name="login" method="login" class="com.itmyhome.Login">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
导出Excel如图:
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
以上是关于Java实现POI导出Excel的主要内容,如果未能解决你的问题,请参考以下文章
java poi xwpf操作word生成一个表格怎么合并单元格,求大神指导!