日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java读取excel模板并复制_POI复制Excel模板并填充数据

發布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java读取excel模板并复制_POI复制Excel模板并填充数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

3、在javaee工程中引入poi的相關jar包,并把excel模板放入相應工程的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);

//讀取了模板內所有sheet內容

XSSFSheet sheet = wb.getSheetAt(0);

//如果這行沒有了,整個公式都不會有自動計算的效果的

sheet.setForceFormulaRecalculation(true);

如果是xls格式的,就改為:

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));

//讀取excel模板

HSSFWorkbook wb = new HSSFWorkbook(fs);

//讀取了模板內所有sheet內容

HSSFSheet sheet = wb.getSheetAt(0);

5、找到相應的數據行,進行數據填充,要從0開始計數,excel中的第1行,讀取的時候是從0開始的,例如我這邊添加表頭,填充歷史客流、年齡等信息進去

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("景區:全部");

}else{

sheet.getRow(1).getCell(0).setCellValue("景區:"+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("導出時間:"+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();

}

//修改模板內容導出新模板

FileOutputStream out = new FileOutputStream(realPath+newFileName);

wb.write(out);

out.close();

7、將文件路徑返回給前端,直接給前端一個文件的url鏈接,讓他自己location.href跳轉就可以拿到文件了

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、導出后效果如下,而且手動在excel中修改數據,圖表可以自動變換

總結

以上是生活随笔為你收集整理的java读取excel模板并复制_POI复制Excel模板并填充数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。