代码语言:javascript复制
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileModelName = "";
Boolean flag = Boolean.valueOf(request.getParameter("_eiExportFlag"));
if (!flag) {
return;
}
String file = request.getParameter("_eiExportFileName");
String type = request.getParameter("_eiExportFiletype");//统一选择2007版本以后的Excel,不做分类
String name = request.getParameter("_eiExportBlockname");
EiInfo tag = EiInfo.parseJSONString(request.getParameter("_eiExportData"));
EiBlock result = tag.getBlock(name);
Map metas = result.getBlockMeta().getMetas();
List<String> l_header = new ArrayList<>();
Iterator iter = metas.keySet().iterator();
while (iter.hasNext()) {
l_header.add(String.valueOf(iter.next()));
}
List<Object> values = Arrays.asList(metas.values().toArray());
if (values == null) {
return;
}
List<HashMap> rows = result.getRows();
XSSFWorkbook wb =new XSSFWorkbook();;
try {
// // 读取了模板内所有sheet内容
wb.createSheet("name");
wb.setSheetName(0,name);
XSSFSheet sheetAt = wb.getSheetAt(0);
XSSFSheet row1 = sheetAt;
XSSFFont titleFont =wb.createFont();
// titleFont.setBold(bold);//是否粗细
titleFont.setFontName("宋体");//字体
titleFont.setFontHeight(13);//字体大小
XSSFCellStyle titleStyle = wb.createCellStyle();
titleStyle.setFont(titleFont);
// 竖向居中
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 横向居中
titleStyle.setAlignment(HorizontalAlignment.CENTER);
// 边框
titleStyle.setBorderBottom(BorderStyle.THIN);
titleStyle.setBorderLeft(BorderStyle.THIN);
titleStyle.setBorderRight(BorderStyle.THIN);
titleStyle.setBorderTop(BorderStyle.THIN);
Row r0 = row1.getRow(0);
if (r0 == null) {
r0 = row1.createRow(0);
}
XSSFCellStyle x1=titleStyle;//表头
x1.setFillForegroundColor(new XSSFColor(new java.awt.Color(166, 166, 166)));
for (int j = 0; j < values.size(); j ) {//excel的第一行赋值
Cell c0 = r0.getCell(j);
if (c0 == null) {
c0 = r0.createCell(j);
}
c0.setCellStyle(x1);
c0.setCellValue(((EiColumn) values.get(j)).getDescName());
}
// XSSFCellStyle xssfCellStyle1 = Font2BlodStyle(wb, false, 12, false);
XSSFCellStyle x2=titleStyle;//表内容
x2.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 255)));
for (int i = 0; i < rows.size(); i ) {
Row r1 = row1.getRow(i 1);
if (r1 == null) {
r1 = row1.createRow(i 1);
}
HashMap<String, String> hashMap = rows.get(i);
int cqw = 0;
for (String s : l_header) {
Cell c0 = r1.getCell(cqw);
if (c0 == null) {
c0 = r1.createCell(cqw);
}
c0.setCellStyle(x2);
c0.setCellValue(hashMap.get(s));
cqw ;
}
}
//自动调整列宽
for (int i = 0; i < values.size(); i ) {
sheetAt.autoSizeColumn(i);
sheetAt.setColumnWidth(i,sheetAt.getColumnWidth(i));
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" new String((file ".xlsx").getBytes(), "iso-8859-1"));
ServletOutputStream sout = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(sout);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
} catch (Exception s) {
System.out.println(s);
}
}
依赖
代码语言:javascript复制 <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>