首页 / JAVA / Java导出Excel
Java导出Excel
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java导出Excel,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4081字,纯文字阅读大概需要6分钟。
内容图文
![Java导出Excel](/upload/InfoBanner/zyjiaocheng/719/758937c8c3994bbcad76218092b6d8f2.jpg)
导出Excel是比较常用的功能,在此记录一下,省去了每次要去网上找代码的时间
1:首先看下数据库 order_info 只有简单的几个列
2:导入maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.6</version>
</dependency>
3:创建ExcelUtil工具类
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
public class ExcelUtil {
/**
* 导出Excel
*
* @param fileName 文件名
* @param sheetName sheet名称
* @param title 表头
* @param data 二维数组格式的数据
* @param response
*/
public static void getHSSFwb(String fileName, String sheetName, String[] title, String data[][], HttpServletResponse response) {
//创建文件、单个表格、行、单元格
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(sheetName);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
//标题Style
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font = wb.createFont(); // 生成一个字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setColor(HSSFColor.WHITE.index);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
//内容Style
HSSFCellStyle style2 = wb.createCellStyle();
style2.setFillForegroundColor(HSSFColor.WHITE.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont font2 = wb.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
style2.setFont(font2);
// 创建表头标题 第一行数据
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//填充数据 二维数组
for (int i = 0; i < data.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < data[i].length; j++) {
cell = row.createCell(j);
cell.setCellValue(data[i][j]);
cell.setCellStyle(style2);
}
}
//浏览器响应
OutputStream output;
try {
output = response.getOutputStream();
response.reset();
//这里如果文件名想要为中文就要fileName=new String(fileName.getBytes(),"ISO8859-1")
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1"));
response.setContentType("application/vnd.ms-excel");
wb.write(output);
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4:这样就可以直接用了,看下Controller的代码吧
@RestController
public class ExcelController {
@Autowired
private OrderInfoService orderInfoService;
@RequestMapping("excel")
public void excel(HttpServletResponse response) {
//select * from order_info
List<OrderInfo> list = orderInfoService.getAllOrderInfo();
String[] title = {"订单Id", "内容", "创建时间", "订单状态"};
String fileName = "订单信息_" + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".xls";
String sheetName = "订单信息";
//二维数组
String[][] data = new String[list.size()][title.length];
for (int i = 0; i < data.length; i++) {
OrderInfo oi = list.get(i);
data[i][0] = oi.getId().toString();
data[i][1] = oi.getContent();
data[i][2] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(oi.getCreateTime());
data[i][3] = oi.getStatus() == 1 ? "已完成" : "其它"; // Java 三元表达式
}
//工具类导出
ExcelUtil.getHSSFwb(fileName, sheetName, title, data, response);
}
}
5:浏览器输入你的controller访问路径,我这里的是 localhost:8080/excel,导出后效果:
内容总结
以上是互联网集市为您收集整理的Java导出Excel全部内容,希望文章能够帮你解决Java导出Excel所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。