Java實現POI匯出Excel

來源:互聯網
上載者:User

Java實現POI匯出Excel

Web架構為Struts2,所用jar包如下:

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.setDefaultColumnWidth(20); // 預設列寬            HSSFFont font = wb.createFont();            font.setFontName(黑體);            font.setFontHeightInPoints((short) 13);// 設定字型大小            font.setColor(HSSFColor.WHITE.index); //字型顏色            HSSFCellStyle headStyle = wb.createCellStyle(); // 頭部樣式            headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平置中            headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下邊框            headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左邊框            headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上邊框            headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右邊框            headStyle.setFillForegroundColor(HSSFColor.TEAL.index);// 設定背景色            headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);            headStyle.setFont(font);// 選擇需要用到的字型格式            HSSFCellStyle contentStyle = wb.createCellStyle(); // 內容樣式            contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下邊框            contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左邊框            contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上邊框            contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右邊框            HSSFRow row_h = sheet.createRow((short) 0);            HSSFCell ch = row_h.createCell(0);            ch.setCellValue(測試匯出Excel);            ch.setCellStyle(headStyle);            sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 2));// 指定合并地區            //表頭            HSSFRow row1 = sheet.createRow((short) 1);            HSSFCell cell_1_0 = row1.createCell(0);            cell_1_0.setCellValue(姓名);            cell_1_0.setCellStyle(headStyle);            HSSFCell cell_1_1 = row1.createCell(1);            cell_1_1.setCellValue(出生日期);            cell_1_1.setCellStyle(headStyle);            HSSFCell cell_1_2 = row1.createCell(2);            cell_1_2.setCellValue(住址);            cell_1_2.setCellStyle(headStyle);            //內容            HSSFRow row2 = sheet.createRow(2);            HSSFCell cell_2_0 = row2.createCell(0);            cell_2_0.setCellValue(itmyhome);            cell_2_0.setCellStyle(contentStyle);            HSSFCell cell_2_1 = row2.createCell(1);            cell_2_1.setCellValue(1990-05-01);            cell_2_1.setCellStyle(contentStyle);            HSSFCell cell_2_2 = row2.createCell(2);            cell_2_2.setCellValue(北京市昌平區);            cell_2_2.setCellStyle(contentStyle);            FileOutputStream fileOut = new FileOutputStream(d:/mysheet.xls); //匯出路徑            wb.write(fileOut);            fileOut.close();            inputStream = new FileInputStream(d:/mysheet.xls); //下載        } catch (Exception e) {            e.printStackTrace();        }        return success;    }    public InputStream getInputStream() {        return inputStream;    }    public void setInputStream(InputStream inputStream) {        this.inputStream = inputStream;    }}
struts.xml
                                 application/octet-streaminputStreamattachment;filename=export.xls4096                                                index.jsp            

匯出Excel

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.