1.export function
//导出文件接口 public String export(){ return this.myExport(exportList); }
2.Export column name
private String myExport(List<BusinessDept> list){ com.bronzesoft.power.tools.json.JSONObject info = new com.bronzesoft.power.tools.json.JSONObject(); try{ List<String> headList = new ArrayList<String>(Arrays.asList("年", "月", "部门","部门负责人","经营值","收入", "支出","填报工时","标准工时","经营参数")); Commonutil.export(list,headList,"部门经营总览导出"); info = Commonutil.setInfo(info,"部门经营总览导出"); }catch (Exception e){ LogUtil.error(this.getClass().getName()+".exportExcel()", e); } return info.toString(); }
3.export implementation method
First, understand the organizational form of an Excel file. The Excel file corresponds to a workbook (XSSFWorkbook). A workbook can be composed of multiple sheets (XSSFSheet), a sheet is composed of multiple rows (XSSFRow), and a row is composed of multiple cells (XSSFCell).
public static <T> boolean export(List<T> list,List<String> headList,String fileName){ return myExport(list,headList,fileName,com.bronzesoft.rdm.platform.util.Constants.PATH + com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR ); } //将list导出为excel,文件名为fileName public static <T> boolean myExport(List<T> list,List<String> headList,String fileName,String path){ try{ if(!checkListAndHead(list,headList)){ LogUtil.info( "head的长度有问题,导出的文件不正确" ); } File file = new File(path + File.separator + fileName + ".xlsx"); if(!file.exists()) { file.createNewFile(); } //创建对应excel文件,存储路径path待确定 XSSFWorkbook workBook = new XSSFWorkbook(); //创建一个excel的sheet页 XSSFSheet sheet = workBook.createSheet(fileName); XSSFRow row = null; XSSFCell cell = null; Map<String, CellStyle> styles = StyleUtil.createStyles(workBook); //表头样式 CellStyle headCenterIndex = styles.get("headCenter"); //列的样式居中,背景颜色为白色 CellStyle center = styles.get("centerWhite"); row = sheet.createRow(0); //表头数据 for (int i = 0; i < headList.size(); i++) { //创建列 cell = row.createCell(i); //设置列的value cell.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 <T> boolean checkListAndHead(List<T> list,List<String> headList){ if(list.size()>0){ T t = list.get(0); if(getColumnCount(t) != headList.size()){ LogUtil.info( "head的长度有问题" ); return false; } } return true; } //获取一个对象成员变量的个数 public static <T> int getColumnCount(T 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(); }
The above is the detailed content of How to export excel files using the export method in java. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool