java写入Excel并读取文件

来源:互联网 发布:费洛伊德算法 编辑:程序博客网 时间:2024/05/01 16:06

java
    /**
     * 返回设备申请基本信息列表
     * @param sourceFile
     * @param targetFile
     * @param undoTasks
     * @return
     */
    public static boolean applyInfo(File sourceFile, File targetFile , List undoTasks) {
        int size = undoTasks.size();
        try {
            Workbook wb = Workbook.getWorkbook(sourceFile);
            WritableWorkbook wwb = Workbook.createWorkbook(targetFile, wb);
            // 读取第一张工作表统计列表
            WritableSheet ws1 = wwb.getSheet(0);
             
            String userName = null;
            String userDepart = null;
            String userPost = null;
            String requestType = null;
            String requestReason = null;
            String requestPurpose = null;
            BpmShenheMenu shenHe = null;
            for(int i=0; i<size; i++) {
                shenHe = (BpmShenheMenu)undoTasks.get(i);
                userName = shenHe.getUserName();
                userDepart = shenHe.getUserDepartName();
                userPost = shenHe.getPostName()==null?"&nbsp;":shenHe.getPostName();
                requestType = shenHe.getRequestTypeName();
                requestReason = shenHe.getRequestReason();
                requestPurpose = shenHe.getReqeustPurposeName();
               
                Label label = new Label(0, (i+1), (i+1)+"", getNormolCell());
                ws1.addCell(label);
                label = new Label(1, (i+1), userName, getNormolCell());
                ws1.addCell(label);
                label = new Label(2, (i+1), userDepart, getNormolCell());
                ws1.addCell(label);
                label = new Label(3, (i+1), userPost, getNormolCell());
                ws1.addCell(label);
                label = new Label(4, (i+1), requestType, getNormolCell());
                ws1.addCell(label);
                label = new Label(5, (i+1), requestReason, getNormolCell());
                ws1.addCell(label);
                label = new Label(6, (i+1), requestPurpose, getNormolCell());
                ws1.addCell(label);
            }
            wwb.write();
            wwb.close();
            wb.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

 



    /**
     * 设置单元格样式
     *
     * @return
     */
    public static WritableCellFormat getNormolCell() {// 12号字体,上下左右居中,带黑色边框
        WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 9);
        WritableCellFormat format = new WritableCellFormat(font);
        try {
            format.setAlignment(jxl.format.Alignment.CENTRE);
            format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
        } catch (WriteException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return format;
    }

 

 

jsp

function downloadEvent(){
            <%
           
            if(hasData) {
                File sourcefile = new File(application.getRealPath("/")
                    + "//reports//sourceFiles//applyInfo.xls");
                File targetfile = new File(application.getRealPath("/")
                    + "//reports//targetFiles//applyInfo.xls");
                boolean readSuccess=false;
                if(targetfile.exists()){
                    targetfile.delete();
                }
                readSuccess=ReadXls.applyInfo(sourcefile, targetfile,undoTasks);
                if(readSuccess && targetfile.exists()){
                %>
                window.open("../reports/targetFiles/applyInfo.xls");
                <%
                }
            } else {
            %>
                alert("无数据!");
            <%
            }
            %>
        }

原创粉丝点击