Use poi to export Excel file and solve Chinese name garbled
the first method:
@Action("subAreaAction_exportXLs")
public String exportXLs() throws IOException{
List<SubArea> list = subAreaService .findAll(); // Query the database to get parameters
// Create the document object of hssfworkbook Excel
HSSFWorkbook wb = new HSSFWorkbook();
// create new sheet
HSSFSheet sheet = wb .createSheet( " Partition Data " );
// set the first line
HSSFRow row = sheet.createRow(0);
// Set the properties of each column of the first row
row .createCell(0).setCellValue( " Partition Number " );
row .createCell(1).setCellValue( " Partition Name " );
row .createCell(2).setCellValue( " Keyword " );
row .createCell(3).setCellValue( " auxiliary keyword " );
row .createCell(4).setCellValue( " province and city " );
// loop through the collection
for (SubArea subArea : list) {
// Add the attribute to a new line
// According to the method of sheet , get the row number of the last row + 1 as the row number of the next row
HSSFRowrows = sheet.createRow(sheet.getLastRowNum()+1);
rows.createCell(0).setCellValue(subArea.getId());
rows.createCell(1).setCellValue(subArea.getStartNum());
rows.createCell(2).setCellValue(subArea.getKeyWords());
rows.createCell(3).setCellValue(subArea.getAssistKeyWords());
rows.createCell(4).setCellValue(subArea.getArea().getName());
}
// output Excel file
// The ssh framework used here , output Excel requires an output stream
// get response
HttpServletResponseresponse = ServletActionContext.getResponse();
// get the output stream
OutputStream output = response.getOutputStream();
response.reset();
// Set the partition Chinese name
String filename = " Partition Information " ;
// Set the encoding of the response
response .setContentType( "application/x-download" ); // The following three lines are the key code to deal with garbled characters
response.setCharacterEncoding("utf-8");
// Set the Content-disposition corresponding to the browser response header
response.setHeader("Content-disposition", "attachment;filename="+new String(filename.getBytes("gbk"), "iso8859-1")+".xls");
// wb output
wb.write(output);
output.close();
returnNONE;
}
The second method:
Use tools
// Download the exel file as an attachment
// File download: one stream (file output stream) and two headers (content format MIME type, file opening method (browser embedded mode development, attachment download attachment ))
String fileName = " Partition data.xls" ;
// handle Chinese problems
// By getting the browser information in the request header
String agent = ServletActionContext.getRequest().getHeader("User-Agent");
// Encode the text according to the browser
fileName = FileUtils.encodeDownloadFilename(fileName, agent);
HttpServletResponse response =ServletActionContext.getResponse();
// set header information
response.setHeader("contentType", "application/vnd.ms-excel");
response.setHeader("content-disposition", "attachment;fileName="+fileName);
OutputStream stream = response.getOutputStream();
workbook.write(stream);
returnNONE;
Tool class code:package cn.itcast.bos.utils;
import java.io.IOException;
import java.net.URLEncoder;
import sun.misc.BASE64Encoder;
public class FileUtils {
/**
* When downloading files, name the attachment for different browsers encoding
*
* @param filename
* download file name
* @param agent
* client browser
* @return encoded download attachment name
* @throws IOException
*/
public static String encodeDownloadFilename(String filename, String agent)
throws IOException {
if (agent.contains("Firefox")) { // Firefox
filename = "=?UTF-8?B?"
+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
+ "?=";
filename = filename.replaceAll("\r\n", "");
} else { // IE及其他浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+"," ");
}
return filename;
}
}