Java poi exports Excel for download to the client
- 2021-01-14 06:00:21
- OfStack
Java poi exports Excel and downloads it to the client. Details are as follows
The Maven configuration, which includes dependencies for other file formats, is all posted
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.8</version>
</dependency>
Service layer
@Override
public void export(Long sblsh, String excelName, OutputStream out) {
try {
// The first 1 Step, create 1 a webbook , corresponding to 1 a Excel file
HSSFWorkbook wb = new HSSFWorkbook();
// generate 1 A table
HSSFSheet sheet = wb.createSheet(excelName);
// The first 3 Step in sheet Adds a table header in 0 line
HSSFRow row = sheet.createRow(0);
// The first 4 Step, create the cell, and set the value table header Set the header to center
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // create 1 The center format
HSSFCell cell = row.createCell(0);
cell.setCellStyle(style);
Byte kjzz = qyjbxxMapper.getKjzz(sblsh);
List<A> record = this.selectBySblsh(sblsh);
this.insertData(wb, sheet, row, record, out);
}
} catch (Exception e) {
logger.info(e.getMessage());
}
}
/**
* Import data into the table
* @param wb execl file
* @param sheet form
* @param row Table row
* @param record The data to be exported
* @param out The output stream
*/
private void insertData(HSSFWorkbook wb,HSSFSheet sheet,HSSFRow row,List<A> record,
OutputStream out){
try {
row = sheet.createRow(1);
for(int i=0;i<title.length;i++){
row.createCell(i).setCellValue(title[i]);
}
for(int i=0;i<record.size();i++){
row = sheet.createRow(i+2);
A data = record.get(i);
row.createCell(0).setCellValue(data.getHc());
row.createCell(1).setCellValue(data.getXm());
BigDecimal je = data.getJe();
if(je!=null){
row.createCell(2).setCellValue(je.doubleValue());
}
}
// Merge cells, front 2 The bits represent the beginning, the end of the line, and the end 2 The bits represent the beginning and end columns
CellRangeAddress region = new CellRangeAddress(0,0,0,title.length-1);
sheet.addMergedRegion(region);
wb.write(out);
out.flush();
out.close();
wb.close();
} catch (Exception e) {
logger.info(e.getMessage());
}
}
Controller
@RequestMapping("/export")
public void export(Long sblsh, HttpServletRequest request, HttpServletResponse response){
response.setContentType("octets/stream");
String excelName = " The file name ";
try {
response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");
OutputStream out = response.getOutputStream();
aService.export(sblsh,excelName ,out);
} catch (Exception e) {
e.printStackTrace();
}
}