Java-based POI export Excel

Source: Internet
Author: User

Java-based POI export Excel

The Web framework is Struts2, And the jar package is as follows:

ExportExcelAction. java
Import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStream; import org. apache. poi. hssf. usermodel. HSSFCell; import org. apache. poi. hssf. usermodel. HSSFCellStyle; import org. apache. poi. hssf. usermodel. HSSFFont; import org. apache. poi. hssf. usermodel. HSSFRow; import org. apache. poi. hssf. usermodel. HSSFSheet; import org. apache. poi. hssf. usermodel. HSSFWorkbook; import org. apache. poi. hssf. util. HSSFColor; import org. apache. poi. ss. util. region; public class ExportExcelAction {private InputStream inputStream; public String export () {try {HSSFWorkbook wb = new HSSFWorkbook (); HSSFSheet sheet = wb. createSheet (new sheet); sheet. setdefacolumcolumnwidth (20); // The default column width HSSFFont font = wb. createFont (); font. setFontName (); font. setFontHeightInPoints (short) 13); // set the font size. setColor (HSSFColor. WHITE. index); // font color HSSFCellStyle headStyle = wb. createCellStyle (); // headStyle. setAlignment (HSSFCellStyle. ALIGN_CENTER); // horizontally centered headStyle. setBorderBottom (HSSFCellStyle. BORDER_THIN); // The headStyle of the lower border. setBorderLeft (HSSFCellStyle. BORDER_THIN); // The Left Border headStyle. setBorderTop (HSSFCellStyle. BORDER_THIN); // The headStyle of the upper border. setBorderRight (HSSFCellStyle. BORDER_THIN); // The headStyle of the right border. setFillForegroundColor (HSSFColor. TEAL. index); // set the background color headStyle. setFillPattern (HSSFCellStyle. SOLID_FOREGROUND); headStyle. setFont (font); // select the font format HSSFCellStyle contentStyle = wb. createCellStyle (); // content style contentStyle. setBorderBottom (HSSFCellStyle. BORDER_THIN); // The contentStyle of the lower border. setBorderLeft (HSSFCellStyle. BORDER_THIN); // contentStyle on the left border. setBorderTop (HSSFCellStyle. BORDER_THIN); // The top frame contentStyle. setBorderRight (HSSFCellStyle. BORDER_THIN); // The Right Border HSSFRow row_h = sheet. createRow (short) 0); HSSFCell ch = row_h.createCell (0); ch. setCellValue (test and export Excel); ch. setCellStyle (headStyle); sheet. addMergedRegion (new Region (0, (short) 0, 0, (short) 2); // specify the merging area // header HSSFRow row1 = sheet. createRow (short) 1); HSSFCell cell_1_0 = row1.createCell (0); cell_00000.setcellvalue (name); Response (headStyle); HSSFCell cell_1_1 = row1.createCell (1); Birth date ); cell_rj1.setcellstyle (headStyle); HSSFCell cell_1_2 = row1.createCell (2); cell_00002.setcellvalue (Address); cell_00002.setcellstyle (headStyle); // content HSSFRow row2 = sheet. createRow (2); HSSFCell cell_2_0 = row2.createCell (0); encrypt (itmyhome); encrypt (contentStyle); HSSFCell cell_2_1 = row2.createCell (1); then ); response (contentStyle); HSSFCell cell_2_2 = row2.createCell (2); cell_2_2.setCellValue (Changping District, Beijing); Response (contentStyle); FileOutputStream fileOut = new FileOutputStream (d:/mysheet.xls ); // export path wb. write (fileOut); fileOut. close (); inputStream = new FileInputStream (d:/mysheet.xls); // download} catch (Exception e) {e. printStackTrace () ;}return success;} public InputStream getInputStream () {return inputStream;} public void setInputStream (InputStream inputStream) {this. inputStream = inputStream ;}}
Struts. xml

      
                                
    
     application/octet-stream
     inputStream
     attachment;filename=export.xls
     4096            
                
       
                       
    
     index.jsp
                
   
  

Export Excel

 

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.