Java handles Excel-related notes and javaexcel notes

Source: Internet
Author: User

Java handles Excel-related notes and javaexcel notes

           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();
// Construct the output stream 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"); // generate a worksheet WritableWorkbook wwb = Workbook. createWorkbook (response. getOutputStream (); WritableSheet sheet = wwb. createS Heet ("task list", 1); // format the text 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); // table size sheet. setColumnView (0, 20); // table header sheet. addCell (new Label (0, 0, "Task Name", format1); // SimpleDateForma T 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);} // close the output stream wwb. write (); wwb. close (); // initialize the worksheet output stream String title = "Export Project"; response. setContentType ("application/vnd. ms-excel "); response. setHeader ("Content-disposition", "attachment; filename =" + new String (title. getBytes (" GB2312 ")," ISO8859_1 ") +". xls "); // The title must be transcoded in Chinese. attachment: the" Save as "dialog box is displayed. inline: open the file with a program known to be associated. WritableWorkbook workbook; OutputStream ouputStream = response. getOutputStream (); workbook = Workbook. createWorkbook (ouputStream); WritableSheet sheet = workbook. createSheet ("project list", 0); sheet. setColumnView (0, 5); // set the jxl width. write. writableFont wfont = new jxl. write. writableFont (WritableFont. createFont (""), 10); // set the WritableCellFormat wc = new WritableCellFormat (wfont); wc. setAlignment (Alignment. CENTRE); // set the center wc. setBackground (jxl. format. colour. LIGHT_GREEN); // set the background color of the cell // worksheet content-Label label sheet. addCell (new jxl. write. label (0, 0, "Serial Number", wc); sheet. addCell (new jxl. write. label (13, 0, "task status", 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. getA CceptDate ();} // close the worksheet output stream 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 ();}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.