Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to Export excel File by export method in java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to export excel files by export in java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1.export function / / Export file interface public String export () {return this.myExport (exportList);} 2. Export the list private String myExport (List list) {com.bronzesoft.power.tools.json.JSONObject info = new com.bronzesoft.power.tools.json.JSONObject (); try {List headList = new ArrayList ("year", "month", "Department", "Department head", "operating value", "income", "Expenditure", "fill in working hours", "Standard working hours", "operating parameters")) Commonutil.export (list,headList, "departmental Business Overview Export"); info = Commonutil.setInfo (info, "departmental Business Overview Export");} catch (Exception e) {LogUtil.error (this.getClass (). GetName () + ".exportExcel ()", e);} return info.toString ();} 3.export implementation method

First of all, understand the organizational form of an Excel file. An Excel file corresponds to a workbook (XSSFWorkbook), a workbook can have multiple sheet (XSSFSheet), a sheet is composed of multiple row (XSSFRow), and a row is composed of multiple cell (XSSFCell).

Public static boolean export (List list,List headList,String fileName) {return myExport (list,headList,fileName,com.bronzesoft.rdm.platform.util.Constants.PATH + com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR);} / / Export list to excel with the file name fileNamepublic static boolean myExport (List list,List headList,String fileName,String path) {try {if (! checkListAndHead (list,headList)) {LogUtil.info ("head has a problem with the length and the exported file is incorrect") } File file = new File (path + File.separator + fileName + ".xlsx"); if (! file.exists ()) {file.createNewFile ();} / create a corresponding excel file with the storage path to be determined XSSFWorkbook workBook = new XSSFWorkbook (); / / create a sheet page of excel XSSFSheet sheet = workBook.createSheet (fileName); XSSFRow row = null;XSSFCell cell = null;Map styles = StyleUtil.createStyles (workBook); / / header style CellStyle headCenterIndex = styles.get ("headCenter") / / the column style is centered, and the background color is white CellStyle center = styles.get ("centerWhite"); row = sheet.createRow (0); / / header data for (int I = 0; I)

< headList.size(); i++) {//创建列cell = row.createCell(i);//设置列的valuecell.setCellValue(headList.get(i));//设置列的样式cell.setCellStyle(headCenterIndex);}//表格数据//写入表格数据String codeName = "";int rownum = 1;T obj = null;for(int i = 0; i < list.size(); i++){obj = list.get(i);if(null != obj){Field[] fields = obj.getClass().getDeclaredFields();row = sheet.createRow(rownum);for(int j = 0; j< fields.length; j++){fields[j].setAccessible(true);codeName = String.valueOf(fields[j].get(obj));//创建第j列cell = row.createCell(j);cell.setCellValue(Commonutil.getStringVal(codeName));cell.setCellStyle(center);}}rownum++;}// //i代表列,设置列的宽度// for (int i = 0; i < headList.size(); i++) {// if(i == 0){// sheet.setColumnWidth(i, 2000);// }else if(i == 1 || i == 3){// sheet.setColumnWidth(i, 6000);// }else{// sheet.setColumnWidth(i, 4000);// }// }//将文件写到临时目录FileOutputStream out = new FileOutputStream(file);workBook.write(out);}catch (Exception e){LogUtil.error(fileName + "export失败" );}return true;}//检查head的size是否符合规范public static boolean checkListAndHead(List list,List headList){if(list.size()>

0) {T t = list.get (0); if (getColumnCount (t)! = headList.size ()) {LogUtil.info ("there is a problem with the length of head"); return false;}} return true;} / / get the number of object member variables public static int getColumnCount (T) {Field [] fields = t.getClass (). GetDeclaredFields (); int count = fields.length;return count;} 4. Front-end docking public static JSONObject setInfo (JSONObject info,String fileName) throws Exception {Storage s = Commonutil.getDefaultStorage (); info.put ("port", String.valueOf (s.getPort (); info.put ("dirAddress", Base64Util.encode (com.bronzesoft.rdm.platform.util.Constants.PATH)); info.put ("address", Base64Util.encode (com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR + File.separator + fileName + ".xlsx"); info.put ("name", Base64Util.encode (fileName)) Info.put ("extendName", "xlsx"); return info;} 5. Front-end code function doExport () {synAjax.tabCall ("com.bronzesoft.rdm.SeDeptTotalTab", "export", null, function (data) {doDownload (data);});} function doDownload (data) {console.log ('data',data) r = eval (' ('+ data+')'); $("# _ file_dir") .val (r.dirAddress); $("# _ file_address") .val (r.address); $("# _ file_name") .val (r.name) $("# _ file_extname") .val (r.extendName); if ($.browser.safari) {$("# _ file_downform") .attr ("target", "");} var servlet = powerPath + "download"; $("# _ file_downform") .attr ("action", servlet) .submit () } this is the end of the article on "how to export excel files by export in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report