本文实例讲述了java实现的导出Excel工具类。分享给大家供大家参考,具体如下:
ExcelExportUtil:
package com.excel;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.HashSet;import java.util.Map;import java.util.Set;import java.util.regex.Pattern;import jxl.Workbook;import jxl.write.Label;import jxl.write.Number;import jxl.write.NumberFormat;import jxl.write.WritableCellFormat;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;import jxl.write.biff.RowsExceededException;public class ExcelExportUtil { public ExcelExportUtil() { } public void export(String sheetName, NumberFormat nf, String[][] content, String[] mergeInfo, OutputStream os, String row, String col) { if (VerifyUtil.isNullObject(content, os) || VerifyUtil.isNull2DArray(content)) { return; } // 默认名称 if (VerifyUtil.isNullObject(sheetName)) { sheetName = "sheet1"; } Set<Integer> rows = this.getInfo(row); Set<Integer> cols = this.getInfo(col); WritableWorkbook workbook = null; try { workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet(sheetName, 0); for (int i = 0; i < content.length; i++) { for (int j = 0; j < content[i].length; j++) { if (content[i][j] == null) { content[i][j] = ""; } if (isNumber(content[i][j]) && !rows.contains(i) && !cols.contains(j)) {// 处理数字 Number number = null; if (VerifyUtil.isNullObject(nf)) {// 数字无格式 number = new Number(j, i, Double.valueOf(content[i][j])); } else {// 如果有格式,按格式生成 jxl.write.WritableCellFormat wcfn = new jxl.write.WritableCellFormat( nf); number = new Number(j, i, Double.valueOf(content[i][j]), wcfn); } sheet.addCell(number); } else {// 处理非数字 WritableCellFormat format = new WritableCellFormat(); if (rows.contains(i) || cols.contains(j)) { format.setAlignment(jxl.format.Alignment.CENTRE); } else { format.setAlignment(jxl.format.Alignment.LEFT); } format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); Label label = new Label(j, i, content[i][j], format); sheet.addCell(label); } } } this.merge(sheet, mergeInfo); workbook.write(); } catch (Exception e) { e.printStackTrace(); } finally { try { workbook.close(); os.close(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void exportFormatExcel(String[][] content, String sheetName, OutputStream os) { if (VerifyUtil.isNullObject(content, os) || VerifyUtil.isNull2DArray(content)) { return; } // 默认名称 if (VerifyUtil.isNullObject(sheetName)) { sheetName = "sheet1"; } WritableWorkbook workbook = null; try { workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet(sheetName, 0); for (int i = 0; i < content.length; i++) { for (int j = 0; j < content[i].length; j++) { if (content[i][j] == null) { content[i][j] = ""; } WritableCellFormat format = new WritableCellFormat(); format.setAlignment(jxl.format.Alignment.LEFT); format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); Label label = new Label(j, i, content[i][j], format); sheet.addCell(label); } } workbook.write(); } catch (Exception e) { e.printStackTrace(); } finally { try { workbook.close(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void exportFormatExcel(Map<String, String[][]> content, String[] salary_name_array, String sheetName, OutputStream os) { if (VerifyUtil.isNullObject(content, os) || content.size() == 0) { return; } // 默认名称 if (VerifyUtil.isNullObject(sheetName)) { sheetName = "sheet1"; } WritableWorkbook workbook = null; try { workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet(sheetName, 0); int index = 0; for (int k = 0; k < salary_name_array.length; k++) { String[][] value = (String[][]) content .get(salary_name_array[k]); if (value != null && value.length > 0) { if (index != 0) { index++; } WritableCellFormat format1 = new WritableCellFormat(); format1.setAlignment(jxl.format.Alignment.LEFT); format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); Label label1 = new Label(0, index, salary_name_array[k], format1); sheet.addCell(label1); for (int i = 0; i < value.length; i++) { index++; for (int j = 0; j < value[i].length; j++) { if (value[i][j] == null) { value[i][j] = ""; } WritableCellFormat format = new WritableCellFormat(); format.setAlignment(jxl.format.Alignment.LEFT); format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); Label label = new Label(j, index, value[i][j], format); sheet.addCell(label); } } } } workbook.write(); } catch (Exception e) { e.printStackTrace(); } finally { try { workbook.close(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void merge(WritableSheet sheet, String[] mergeInfo) throws RowsExceededException, NumberFormatException, WriteException { if (VerifyUtil.isNullObject(sheet) || VerifyUtil.isNull1DArray(mergeInfo)) { return; } else if (!this.isMergeInfo(mergeInfo)) { return; } else { for (String str : mergeInfo) { String[] temp = str.split(","); sheet.mergeCells(Integer.parseInt(temp[1]), Integer.parseInt(temp[0]), Integer.parseInt(temp[3]), Integer.parseInt(temp[2])); } } } private Set<Integer> getInfo(String indexes) { Set<Integer> set = new HashSet<Integer>(); if (VerifyUtil.isNullObject(indexes)) { return set; } String[] temp = indexes.split(",", 0); for (String str : temp) { if (isNumeric(str)) { set.add(Integer.parseInt(str)); } } return set; } private boolean isNumeric(String str) { if (VerifyUtil.isNullObject(str)) { return false; } Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } private boolean isNumber(String number) { // 判断参数 if (VerifyUtil.isNullObject(number)) { return false; } // 查看是否有小数点 int index = number.indexOf("."); if (index < 0) { return isNumeric(number); } else { // 如果有多个".",则不是数字 if (number.indexOf(".") != number.lastIndexOf(".")) { return false; } String num1 = number.substring(0, index); String num2 = number.substring(index + 1); return isNumeric(num1) && isNumeric(num2); } } private boolean isMergeInfo(String[] mergeInfo) { if (VerifyUtil.isNull1DArray(mergeInfo)) { return false; } else { for (String str : mergeInfo) { String[] temp = str.split(","); if (VerifyUtil.isNull1DArray(temp) || temp.length != 4) { return false; } else { for (String s : temp) { if (!isNumeric(s)) { return false; } } } } } return true; } public static void main(String[] args) { ExcelExportUtil ee = new ExcelExportUtil(); String[][] content = new String[][] { { "", "第一列", null, "第三列" }, { "第一行", "aa", "2.00", "22" }, { "第二行", "bb", "3.01", "32" }, { "第三行", "cc", "4.00", "41" } }; try { OutputStream os = new FileOutputStream("D:/test2.xls"); // ee.export(null,null, content,null, os); ee.export(null, null, content, new String[] { "0,1,0,2", "1,0,3,0" }, os, "0,1", "0"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1142
183.71 KB下载数642
644.84 KB下载数2755
相关文章
发现更多好内容猜你喜欢
AI推送时光机java实现的导出Excel工具类实例
后端开发2023-05-31
Java导出Excel通用工具类实例代码
后端开发2024-04-02
Java利用Reflect实现封装Excel导出工具类
后端开发2022-11-13
Java使用excel工具类导出对象功能示例
后端开发2023-05-30
怎么在Java中导出Excel通用工具类
后端开发2023-06-14
Java+element实现excel的导入和导出
后端开发2023-05-16
Java实现导出Excel功能
后端开发2024-04-02
Java用POI导入导出Excel实例分析
后端开发2024-04-02
使用Java导入、导出excel详解(附有封装好的工具类)
后端开发2023-08-18
JavaScript实现导入导出excel的示例代码
后端开发2024-04-02
在java poi导入Excel通用工具类示例详解
后端开发2023-05-31
java如何实现Excel的导入、导出操作
后端开发2023-05-31
vue实现excel表格的导入导出的示例
后端开发2023-05-15
java实现excel的导出之使用easyExcel
后端开发2023-08-18
JavaHutool工具实现验证码生成及Excel文件的导入和导出
后端开发2024-04-02
Java实现Excel导入导出操作详解
后端开发2024-04-02
JAVA怎么实现导出Excel功能
后端开发2023-10-08
Java怎么实现Excel导入导出操作
后端开发2023-06-29
Java如何实现导出Excel功能
后端开发2023-06-21
咦!没有更多了?去看看其它编程学习网 内容吧