导出Excel(可设置背景、字体颜色、对齐方式、列的宽度)

tech2022-12-26  113

String fileName = "企业信息确认.xls"; String title = "污染源名称,污染源地址,所属省份,所属地市,所属区县,"; QueryHelper qHelper = new QueryHelper(querySql); JdbcDAO dao = new JdbcDAO(); List<DynaBean> list = dao.getListValue(null, qHelper); // 以下开始输出到EXCEL //定义输出流,以便打开保存对话框______________________begin OutputStream os = response.getOutputStream();// 取得输出流 response.reset();// 清空输出流 response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1")); // 设定输出文件头 response.setContentType("application/msexcel");// 定义输出类型 //定义输出流,以便打开保存对话框_______________________end /** **********创建工作簿************ */ WritableWorkbook workbook = Workbook.createWorkbook(os); /** **********创建工作表************ */ WritableSheet sheet = workbook.createSheet("Sheet1", 0); //分别给0列设置不同的宽度; sheet.setColumnView(0, 25); sheet.setColumnView(1, 25); sheet.setColumnView(2, 15); //给sheet电子版中所有的列设置默认的列的宽度; sheet.getSettings().setDefaultColumnWidth(10); /** **********设置纵横打印(默认为纵打)、打印纸***************** */ jxl.SheetSettings sheetset = sheet.getSettings(); sheetset.setProtected(false); sheetset.setVerticalFreeze(1);//冻结第一行标题 /** ************设置单元格字体************** */ WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10); WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD); /** ************以下设置三种单元格样式,灵活备用************ */ // 用于标题居中 WritableCellFormat wcf_center = new WritableCellFormat(BoldFont); wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条 wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐 wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐 wcf_center.setWrap(true); // 文字是否换行 wcf_center.setBackground(Colour.TURQOISE2); // 用于正文居左 WritableCellFormat wcf_left = new WritableCellFormat(NormalFont); wcf_left.setBorder(Border.ALL, BorderLineStyle.MEDIUM); // 线条 wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐 wcf_left.setAlignment(Alignment.CENTRE); // 文字水平对齐 wcf_left.setWrap(true); // 文字是否换行 /** ***************以下是EXCEL开头大标题,暂时省略********************* */ //sheet.mergeCells(0, 0, colWidth, 0); //sheet.addCell(new Label(0, 0, "XX报表", wcf_center)); /** ***************以下是EXCEL第一行列标题********************* */ //在Label对象的构造子中指名单元格位置是第一列第一行(0,0) //以及单元格内容为test Label title1 = new Label(0,0, str, wcf_center); //合并:第0列第0行 到 第5列第0行 sheet.mergeCells(0, 0, 30, 0); sheet.addCell(title1); title = title.substring(0, title.length() - 1); for (int i = 0; i < title.split(",").length; i++) { if (title.split(",")[i] != null && !"".equals(title.split(",")[i])) { sheet.addCell(new Label(i, 1, title.split(",")[i], wcf_center)); } } /** ***************以下是EXCEL正文数据********************* */ int j = 2; for (int i = 1; i < list.size(); i++) { DynaBean bean = list.get(i); sheet.addCell(new Label(0, j, BeanUtils.getString(bean, "WRYMC"), wcf_left)); sheet.addCell(new Label(1, j, BeanUtils.getString(bean, "WRYDZ"), wcf_left)); sheet.addCell(new Label(2, j, BeanUtils.getString(bean, "SSSF"), wcf_left)); sheet.addCell(new Label(3, j, BeanUtils.getString(bean, "SSDS"), wcf_left)); sheet.addCell(new Label(4, j, BeanUtils.getString(bean, "SSQX"), wcf_left)); sheet.addCell(new Label(5, j, BeanUtils.getString(bean, "LXR"), wcf_left)); j++; } /** **********将以上缓存中的内容写到EXCEL文件中******** */ workbook.write(); /** *********关闭文件************* */ workbook.close(); os.close();
最新回复(0)