java读取excel模板并复制_POI复制Excel模板并填充数据
3、在javaee工程中引入poi的相關(guān)jar包,并把excel模板放入相應(yīng)工程的WEB-INF目錄下,名稱換為temple.xlsx
org.apache.poi
poi-ooxml
3.12
org.apache.poi
poi
3.12
org.apache.poi
poi-ooxml-schemas
3.12
4、讀取模板文件,這里讀取的是xlsx方式的,
//excel模板路徑
File f = new File(this.getClass().getResource("/").getPath());
String filePath = f+File.separator+"WEB-INF"+File.separator+"temple.xlsx";
File file = new File(filePath);
FileInputStream in =new FileInputStream(file);
//讀取excel模板
XSSFWorkbook wb = new XSSFWorkbook(in);
//讀取了模板內(nèi)所有sheet內(nèi)容
XSSFSheet sheet = wb.getSheetAt(0);
//如果這行沒有了,整個公式都不會有自動計(jì)算的效果的
sheet.setForceFormulaRecalculation(true);
如果是xls格式的,就改為:
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
//讀取excel模板
HSSFWorkbook wb = new HSSFWorkbook(fs);
//讀取了模板內(nèi)所有sheet內(nèi)容
HSSFSheet sheet = wb.getSheetAt(0);
5、找到相應(yīng)的數(shù)據(jù)行,進(jìn)行數(shù)據(jù)填充,要從0開始計(jì)數(shù),excel中的第1行,讀取的時候是從0開始的,例如我這邊添加表頭,填充歷史客流、年齡等信息進(jìn)去
Calendar cal = Calendar.getInstance();
String publishTime = DateUtil.format(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
if(null==name){
sheet.getRow(1).getCell(0).setCellValue("景區(qū):全部");
}else{
sheet.getRow(1).getCell(0).setCellValue("景區(qū):"+name);
}
if(null==beginTime){
sheet.getRow(1).getCell(2).setCellValue("時間:全部");
}else{
sheet.getRow(1).getCell(2).setCellValue("時間:"+beginTime+"~"+endTime);
}
sheet.getRow(1).getCell(6).setCellValue("導(dǎo)出時間:"+publishTime);
//歷史客流
int t1=3;
Map param = initParam(null,beginTime, endTime);
List> daycountlist = dayCountService.selectHistoryDayCountList(null,null,null,name,param.get("beginTime"), param.get("endTime"),12);
for (Map map2 : daycountlist) {
for (Map.Entry m : map2.entrySet()) {
sheet.getRow(t1).getCell(1).setCellValue(m.getKey());
sheet.getRow(t1).getCell(2).setCellValue(Integer.parseInt(m.getValue().toString()));
t1++;
}
}
//性別
Map sexInfo = sexCountService.selectContrastsInfo(name, beginTime, endTime);
int s1=30;
for (Object v : sexInfo.values()) {
sheet.getRow(s1).getCell(2).setCellValue(Double.parseDouble(v.toString())/100.0);
s1++;
}
//年齡
Map ageInfo = ageCountService.selectContrastsInfo(name, beginTime, endTime);
int s2=33;
for (Object v : ageInfo.values()) {
sheet.getRow(s2).getCell(2).setCellValue(Double.parseDouble(v.toString())/100.0);
s2++;
}
6、保存文件
// 保存文件的路徑
String realPath = "/mnt/app/tomcat/webapps/uploadfile/";
if(null==name){
name="全部";
}
String newFileName = "report-" +name+"-"+ DateUtil.getAllTime()+ ".xlsx";
// 判斷路徑是否存在
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdirs();
}
//修改模板內(nèi)容導(dǎo)出新模板
FileOutputStream out = new FileOutputStream(realPath+newFileName);
wb.write(out);
out.close();
7、將文件路徑返回給前端,直接給前端一個文件的url鏈接,讓他自己location.href跳轉(zhuǎn)就可以拿到文件了
map.put("url", "/uploadfile/"+newFileName);
或者也可以使用response返回
//返回文件給前端
FileUtil.downloadFiles(response, realPath+newFileName);
public static void downloadFiles(HttpServletResponse response,
String filePath) {
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
FileInputStream fs = null;
BufferedInputStream buff = null;
OutputStream myout = null;
try {
File file = new File(filePath.trim());
if (file.exists()) {
String fileName = file.getName();
fs = new FileInputStream(file);
response.addHeader(
"Content-Disposition",
"attachment;filename="
+ URLEncoder.encode(fileName, "UTF-8"));
buff = new BufferedInputStream(fs);
byte[] b = new byte[1024];
long k = 0;
myout = response.getOutputStream();
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
buff.close();
} else {
PrintWriter os = response.getWriter();
os.write("文件不存在");
os.close();
}
if (myout != null) {
myout.flush();
myout.close();
}
if (fs != null) {
fs.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (myout != null) {
try {
myout.flush();
myout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
8、導(dǎo)出后效果如下,而且手動在excel中修改數(shù)據(jù),圖表可以自動變換
總結(jié)
以上是生活随笔為你收集整理的java读取excel模板并复制_POI复制Excel模板并填充数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设置Jupyter notebook 默
- 下一篇: 高一计算机专业班主任工作总结,2016学