JAVA操作Excel表
2016-01-06 22:51
387 查看
报表:操作Excel文件
一、需求分析在项目中,我们时常会遇到需要将数据库中查询出来的数据导出到Excel表中,并下载到本地。
二、步骤
在进行Excel表操作时,最常用的方式是采用JExcel来操作。
首先在myeclipse下建一个Java项目,然后导入jxl.jar包。接下来我们就来玩Excel表的操作吧。
三、编写java代码操作Excel表
public class ExcelUtil { private String pathRoot = "F:/files";//文件保存根路径 private String sheetName = "表1";//Excel表名称 //创建一个Excel表,并保存到指定的路径 public void createExcel(String filePath,String fileName) { //将文件根路径+通过文件名hash算法得到的前两位作为文件保存路径一部分 filePath += filePath+this.gainHashCodeFilePath(filePath); //根据指定路径创建,并得到该文件 File file = this.gainFileBean(filePath,fileName,HandName.normal); //创建一个Excel工作薄 WritableWorkBook book = WorkBook.createWorkBook(file); //创建一个Excel工作表 WritableSheet sheet = book.createSheet(sheetName,0);//0表示第一页 /*************格式化单元格start***************/ //1.设置单元格内字体样式【WritableFont类】 WritableFont font1 = new WritableFont(WritableFont.ARIAL,18,WritableFont.BOLD);//18号、加粗、Arial字体(用于title) WritableFont font2 = new WritableFont(WritableFont.ARIAL,12);//用于默认字体大小 --------------------------------------------------- //2.设置单元格样式【WritableCellFormat类】 >>样式1: WritableCellFormat cf_center = new WritableCellFormat(font1); cf_center.setAlignment(Alignment.CENTER);//单元格水平方向居中 cf_center.setVerticalAlignment(VerticalAlignment.CENTER);//垂直方向居中 cf_center.setWrap(false);//不自动换行 --------------------------------------------------- >>样式2: WritableCellFormat cf_left = new WritableCellFormat(font2); cf_center.setAlignment(Alignment.LEFT);//单元格水平方向居左 cf_center.setVerticalAlignment(VerticalAlignment.CENTER);//垂直方向居中 cf_center.setWrap(false);//不自动换行 --------------------------------------------------- //3.设置单元格的合并【调用mergeCells(c1,r1,c2,r2)方法】 sheet.mergeCells(0,1,10,2);//将第0列第1行 到 第10列第2行的单元格合并成一行 --------------------------------------------------- //4.设置单元格的行高(Row),列宽(Column) sheet.setRowView(1,400);//设置第一行,行高为400/20=20磅 sheet.setColumn(2,20);//设置第二列,列宽为20个字符 /*************格式化单元格end***************/ //将单元格添加到工作表中 Label label = new Label(1,2,"张三",cf_left);//Label类操作的内容为字符串 /*设置第一列,第二行单元格中内容为:张三,采用cf_left样式*/ sheet.addCell(label); Number number = new Number(0,2,6,cf_center);//Number类用于操作的内容为数字 sheet.addCell(number); 。。。。 //将填充好的工作表写入准备好的指定路径的文件中 book.write(); book.close(); -------------------------------------------------- *通过Java将数据导出Excel文件大致情况如此,本代码块够完成其基本功能* } /** * 文件名hash打乱方法 * @param fileName 文件名 * @return 文件保存路径 */ public String gainHashCodeFilePath(String fileName) { String hashCode = Integer.toHexString(fileName.hashCode()).toUpperCase(); String hashCodeFilePath = hashCode.charAt(0)+"/"+hashCode.charAt(1); System.out.println(hashCodeFilePath); return hashCodeFilePath; } /** * * @param filePath 文件上传路径 * @param fileName 文件名 * @param handName normal(不处理文件名)| uuid(uuid_文件名方式)| nowtime(当前时间的10位整数值) * @return */ public File gainFileBean(String filePath,String fileName, HandName handName) { filePath += this.gainHashCodeFilePath(fileName); File file = new File(filePath); if(!file.exists()) { file.mkdirs(); } if(handName.equals(HandName.nowtime)) { fileName = TimeUtil.gain10Time(); } else if(handName.equals(HandName.uuid)) { fileName = UUIDUtil.gainUUIDName(fileName); } else {} return new File(file, fileName+".xls"); } }
相关文章推荐
- 从头认识java-16.5 nio的数据转换
- 【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3
- 在eclipse中设计BPMN 2.0工作流定义的根本步骤
- Spring事务学习
- Windows配置Android环境(eclipse+adt+sdk+genymotion)
- java调用7zip demo
- JAVA堆外内存
- java线程池学习(六)——线程池生命周期管理
- Struts2防止表单重复提交
- Struts2防止表单重复提交
- 《java并发编程实战》读书笔记——并发应用
- JDK配置步骤
- 4. 【创建和销毁对象】通过私有化构造方法强化不可实例化的能力
- JDBC之模型层的完成
- Java下载文件的几种方式
- 排序
- spring mvc 插入一条数据 返回该数据的主键编号
- 《Spring实战》学习笔记-第二章:装配Bean
- Java基础学习5_数组
- Java基础学习4_流程控制语句