import com.genshuixue.coursetool.dao.JDBCUtil; import com.genshuixue.coursetool.entity.CourseEntity; import jxl.Workbook; import jxl.format.VerticalAlignment; import jxl.write.*; import jxl.write.biff.JxlWriteException; import org.springframework.stereotype.Service; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @Service public class ExcelTool { /** * 测试 导出xls文件的表头 */ public static final String[] TestToXls = {"分类", "课程id", "课程名", "单价", "销量"}; public String toExcel() { //这里为导出文件存放的路径 + UUID.randomUUID() String filePath = "/Users/Desktop/"; //加入一个uuid随机数是因为 //每次导出的时候,如果文件存在了,会将其覆盖掉,这里是保存所有的文件 File file = new File(filePath); if (!file.exists()) { file.mkdirs(); } SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd_HH:mm"); // 给要导出的文件起名为 "测试导出数据表_时间.xls" String excelPath = filePath + "genshuixue-" + fmt.format(new Date()) + ".xls"; WritableWorkbook wb = null; try { File file2 = new File(excelPath); //不存在,创建 if (!file2.exists()) { file2.createNewFile(); } //创建xls表格文件 wb = Workbook.createWorkbook(file2); // 表头显示 WritableCellFormat wcf = new WritableCellFormat(); // 水平居中 // wcf.setAlignment(GroupLayout.Alignment.CENTER); wcf.setWrap(true); // 垂直居中 wcf.setVerticalAlignment(VerticalAlignment.CENTRE); // 表头字体 加粗 18号 wcf.setFont(new WritableFont(WritableFont.TIMES, 15, WritableFont.BOLD)); wcf.setBackground(jxl.format.Colour.PERIWINKLE); // 内容显示 WritableCellFormat wcf2 = new WritableCellFormat(); //设置单元格可以换行 wcf2.setWrap(true); //水平居中 // wcf2.setAlignment(GroupLayout.Alignment.CENTER); // 垂直居中 wcf2.setVerticalAlignment(VerticalAlignment.CENTRE); // 内容字体 11号 wcf2.setFont(new WritableFont(WritableFont.TIMES, 11)); //导出的xls的第一页,第二页就是0换成1,“sheet1”,也可以修改为自己想要的显示的内容 WritableSheet ws = wb.createSheet("sheet1", 0); //第2个sheet页 //WritableSheet ws2 = wb.createSheet("sheet2", 1); //代表着表格中第一列的第一行显示查询结果几个字 //ws.addCell(new Label(0, 0, "导出结果")); // 导出时生成表头 for (int i = 0; i < TestToXls.length; i++) { //i,代表的第几列,1,代表第2行,第三个参数为要显示的内容,第四个参数,为内容格式设置(按照wcf的格式显示) //在sheet1中循环加入表头 ws.addCell(new Label(i, 1, TestToXls[i], wcf)); } Connection conn = JDBCUtil.getConn(); // couseId 传null 代表查询全部课程 List<CourseEntity> courseList = JDBCUtil.getCourse(conn, null); int k = 2;//从第三行开始写入数据 for (int i = 0; i < courseList.size(); i++) { // 依次获取分类、课程id、课程名、单价、销量 String categoryName = courseList.get(i).getCategoryName(); String courseId = courseList.get(i).getCourseId() + ""; String courseName = courseList.get(i).getCourseName(); ws.addCell(new Label(0, k, courseList.get(i).getCategoryName(), wcf2)); ws.addCell(new Label(1, k, courseId, wcf2)); ws.addCell(new Label(2, k, courseName, wcf2)); ws.addCell(new Label(3, k, courseList.get(i).getCoursePrice() + "", wcf2)); ws.addCell(new Label(4, k, courseList.get(i).getStuNumber() + "", wcf2)); //ws.mergeCells(4, 5, 5, 5);//合并两列,按参数顺序,意思是第4列的第五行,跟第五列的第五行合并为一个单元格 ws.setColumnView(1, courseId.length() * 2); ws.setColumnView(2, courseName.length() * 2); k++; } //写入,到这里已经生成完成,可以在相应目录下找到刚才生成的文件 wb.write(); // JDBCUtil.closeConnection(conn); } catch (IOException e) { e.printStackTrace(); } catch (JxlWriteException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (wb != null) { wb.close(); } } catch (WriteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return excelPath; } }
导出Excel
最新推荐文章于 2024-09-21 09:49:54 发布