jeecg derived from the custom templates

// get 1 template path:

String lujing = request.getSession().getServletContext().getRealPath("/")+ "export\\template\\test.xlsx";

// 2. Prepare data (a List <Map <String, Object >> Object)

List <the Map <String, Object >> = jdbcTemplate.queryForList Maps (SQL);
.. 3 // read and write InputStream stream in XSSFWorkbook

In InputStream;
the try {
in = new new FileInputStream (new new File (lujing1));
XSSFWorkbook Work = null;
Work = new new XSSFWorkbook (in);
. // 4 Ba map in the value of re-write Excel template

// get the first sheet

XSSFSheet sheetAt = work.getSheetAt(0);

// assignment starts with the third row
XSSFRow row = sheetAt.createRow (i + 2 );

row.createCell(0).setCellValue("");

// ...... assignments in cycles

// 5. Export foreground some of the settings

response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel"); //保证不乱码

Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("MMddHHmmss");
String time="bb"+format.format(date)+".xlsx";//导出的Excel的名字
response.setHeader("Content-Disposition","attachment;" + " filename=" + new String(time.getBytes("utf-8"), "ISO-8859-1"));
ByteArrayOutputStream oss =new ByteArrayOutputStream();
OutputStream os = response.getOutputStream();
work.write(oss);

byte temp[] = oss.toByteArray();
ByteArrayInputStream in1 = new ByteArrayInputStream(temp);
int n = 0;
while ((n = in1.read(temp)) >0) {
os.write(temp, 0, n);
}
os.flush();
os.close(); 

} catch (Exception e) {

e.printStackTrace ();
}

 

pom.xml dependent jar package introduced: // attached

<!-- poi start -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>jeasypoi-base</artifactId>
<version>${jeasypoi.version}</version>
<exclusions>
<exclusion>
<groupId>org.jeecgframework</groupId>
<artifactId>jeasypoi-annotation</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>jeasypoi-web</artifactId>
<version>${jeasypoi.version}</version>
<exclusions>
<exclusion>
<groupId>org.jeecgframework</groupId>
<artifactId>jeasypoi-base</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>jeasypoi-annotation</artifactId>
<version>${jeasypoi.version}</version>
</dependency>
<!-- poi end-->

 

Guess you like

Origin www.cnblogs.com/xujiating/p/10944720.html