JAVA操作Excel表

来源:互联网 发布:软件周末培训班 编辑:程序博客网 时间:2024/06/04 00:51

报表:操作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");    }}
0 0