博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jxl导出excel文件
阅读量:2505 次
发布时间:2019-05-11

本文共 4542 字,大约阅读时间需要 15 分钟。

所需要的第三方jar包:jxl.jar

package test;import java.io.FileNotFoundException;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.OutputStream;  import java.util.ArrayList;  import java.util.Date;  import java.util.List;  import jxl.Workbook;  import jxl.format.Alignment;  import jxl.format.Border;  import jxl.format.BorderLineStyle;  import jxl.format.Colour;  import jxl.format.Pattern;  import jxl.format.UnderlineStyle;  import jxl.format.VerticalAlignment;  import jxl.write.Label;  import jxl.write.WritableCellFormat;  import jxl.write.WritableFont;  import jxl.write.WritableSheet;  import jxl.write.WritableWorkbook;  import jxl.write.WriteException;  /** * @author cyq * 导出 excel */public class ExcelExport {
public static void main(String[] args) { List
list = new ArrayList
(); list.add(new String[] { "a", "b", "c" }); list.add(new String[] { "d", "e", "f" }); createExcel(list, new String[] { "ID", "名称", "时间" }, "G:\\", String.valueOf(new Date().getTime())); } /** * 生成Excel * @param models 数据List
* @param colNames 成Excel的实体列名 * @param tempPath 导出路径 * @param excelName 生成的Excel名 */ public static void createExcel(List
models, String[] colNames, String tempPath, String excelName) { try { OutputStream os = new FileOutputStream(tempPath + "\\" + excelName + ".xls"); WritableWorkbook workbook = Workbook.createWorkbook(os); /*createSheet 传入的int型参数代表sheet号,0是第一页,1是第二页,依次类推,打开Excel表格在底端可以看到,编号最小的页在最左边。 如果在使用createSheet函数的时候没有注意编号问题,两次使用了同一个编号,比如两次创建编号为0的sheet,这时第二次创建的sheet会是第一页,但第一次创建的sheet并未被覆盖,而是向后移成为第二页,后面的页也都后移一页,有些像数组的插入。 另外如果使用了不连续的编号,比如依次创建了编号为0,1,2的sheet,接着创建了编号为4的sheet,这时最后创建的一个sheet的编号不会是4,而是会被设置为顺延的3,如果你执行getSheet(4),会报数组越界的错 */ WritableSheet sheet = workbook.createSheet(excelName, 0); // 设置标题 WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 17, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.WHITE); WritableCellFormat wcf_title = new WritableCellFormat(titleFont); wcf_title.setBackground(Colour.TEAL, Pattern.SOLID); wcf_title.setBorder(Border.ALL, BorderLineStyle.DOUBLE, Colour.OCEAN_BLUE); wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐 wcf_title.setAlignment(Alignment.CENTRE); // 设置正文 WritableFont NormalFont = new WritableFont(WritableFont.TAHOMA, 11); WritableCellFormat wcf_center = new WritableCellFormat(NormalFont); wcf_center.setBorder(Border.ALL, BorderLineStyle.DOUBLE, Colour.GRAY_25); wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐 wcf_center.setAlignment(Alignment.CENTRE); wcf_center.setWrap(true); // 是否换行 sheet.addCell(new Label(0, 0, excelName, wcf_title)); sheet.mergeCells(0, 0, colNames.length, 0); //设置列名 for (int i = 0; i < colNames.length; i++) { sheet.setColumnView(i, colNames[i].length() * 5); sheet.addCell(new Label(i, 1, colNames[i], wcf_center)); } int rowId = 2;// 写入第几行 第一行为列头 数据从第二行开始写 //导入数据集 for (Object ssTopModel : models) { int columnId = 0;// 写入第几列 第一列为自动计算的行号 数据从第二列开始写 // 获取该类 并获取自身方法 String[] strs = (String[]) ssTopModel; for (int i = 0; i < strs.length; i++) { try { sheet.addCell(new Label(columnId, rowId, strs[i], wcf_center)); } catch (Exception e) { e.printStackTrace(); } columnId++; } rowId++; } workbook.write(); workbook.close(); os.flush(); os.close(); } catch (WriteException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

转载地址:http://szmgb.baihongyu.com/

你可能感兴趣的文章
nodejs adm-zip 解压文件 中文文件名乱码 问题解决
查看>>
MapReduce-文本输入
查看>>
在Linux中简单实现回收子进程
查看>>
<Bootstrap> 学习笔记六. 栅格系统使用案例
查看>>
学习blus老师js(6)--js运动基础
查看>>
谈谈架构非功能性
查看>>
【timeisprecious】【JavaScript 】JavaScript RegExp 对象
查看>>
How to set colors of HTML tables
查看>>
Cannot parse POST parameters of request: '<URL>'
查看>>
Hibernate 关联映射
查看>>
PHP语法2
查看>>
python unittest学习1---重要的几个概念
查看>>
MapReduce编程之Reduce Join多种应用场景与使用
查看>>
干货: 可视化项目实战经验分享,轻松玩转 Bokeh (建议收藏)
查看>>
使用pyinstaller打包多个py文件为一个EXE文件
查看>>
书接前文,用多进程模式实现fibonnachi并发计算
查看>>
numpy的数组常用运算练习
查看>>
ExtJs之DHTML,DOM,EXTJS的事件绑定区别
查看>>
Leetcode:Toeplitz Matrix
查看>>
js定时器
查看>>