你的位置:首页 > Java教程

[Java教程]java处理Excel相关笔记


      String name = file.getFilename();          name = name.replaceAll("(&|=|,)", "");      response.setHeader("Content-Disposition", "Attachment; Filename=" + name);      response.setContentType(file.getContentType());      IOUtils.copy(file.getInputStream(), response.getOutputStream());     response.flushBuffer();

      // 构建输出流      response.reset();      final String attachmentHeader = "Attachment; Filename*=utf-8''"          + URLEncoder.encode(project.getName() + ".xls", "UTF-8").replaceAll("\\+", "%20");      response.setHeader("Content-disposition", attachmentHeader);      response.setContentType("application/msexcel");      // 生成工作表      WritableWorkbook wwb = Workbook.createWorkbook(response.getOutputStream());      WritableSheet sheet = wwb.createSheet("任务列表", 1);      // 格式化文本      WritableFont font1 = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);      WritableCellFormat format1 = new WritableCellFormat(font1);      format1.setAlignment(jxl.format.Alignment.CENTRE);      format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);          // 表格大小          sheet.setColumnView(0, 20);         // 表头      sheet.addCell(new Label(0, 0, "任务名称", format1));          // 任务列表      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm");      for (int i = 1; i <= taskList.size(); i++) {        ProjectTask pt = taskList.get(i - 1);        sheet.addCell(new Label(0, i, pt.getName(), format2));       }       // 关闭输出流      wwb.write();      wwb.close();       //初始化工作表输出流      String title="工程项目导出";      response.setContentType("application/vnd.ms-excel");        response.setHeader("Content-disposition", "attachment;filename="+new String(title.getBytes("GB2312"),"ISO8859_1") +".xls");             //标题有中文必须转码 ,attachment:显示另存为对话框inline:尝试用已知关联的程序打开。      WritableWorkbook workbook;      OutputStream ouputStream = response.getOutputStream();           workbook = Workbook.createWorkbook(ouputStream);            WritableSheet sheet = workbook.createSheet("工程列表", 0);        sheet.setColumnView(0, 5);  //设置宽度           jxl.write.WritableFont wfont = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10);//设置字体      WritableCellFormat wc = new WritableCellFormat(wfont);             wc.setAlignment(Alignment.CENTRE);         // 设置居中       wc.setBackground(jxl.format.Colour.LIGHT_GREEN); // 设置单元格的背景颜色            //工作表内容 - Label label      sheet.addCell(new jxl.write.Label(0, 0,"序号",wc));             sheet.addCell(new jxl.write.Label(13, 0,"任务状态",wc));            for (int i = 1; i <= omProjectList.size(); i++) {        OmProject omProject = omProjectList.get(i-1);        sheet.addCell(new jxl.write.Number(0, i,i));         sheet.addCell(new jxl.write.Label(1, i,omProject.getAcceptDate()));               }      //关闭工作表输出流      workbook.write();      workbook.close();      ouputStream.flush();        ouputStream.close();         FileInputStream inputStream = new FileInputStream(importFile);      FileOutputStream outputStream = new FileOutputStream(filePath);      byte[] buf = new byte[1024];      int length = 0;      while ((length = inputStream.read(buf)) != -1) {        outputStream.write(buf, 0, length);      }      inputStream.close();      outputStream.flush();  Workbook workbook = Workbook.getWorkbook(new File(filePath));    Sheet[] sheets = workbook.getSheets();      if (sheets != null && sheets.length > 0) {        Sheet sheet = sheets[0];        int rowSize = sheet.getRows();                for (int j = 1; j < rowSize; j++) {        Cell[] psCells = sheet.getRow(j);                String content = psCells[k].getContents();   }