Apache POI 應用另一篇

來源:互聯網
上載者:User
不說了,直接貼代碼
package test;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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;

public class POI_ExcelDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        /**
         * author youjianbo
         * date 2008-6-26
         * 使用Apache POI產生Excel
         * */
        try
        {
            POI_ExcelDemo excelDemo = new POI_ExcelDemo();
            String []values = {"a","b","c","d","e","f","j"};
            //建立活頁簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //建立Excel 頁
            HSSFSheet sheet = workbook.createSheet();
            excelDemo.setColumnWidth(sheet);
            //給sheet取名,並設定編碼
            workbook.setSheetName(0, "第一張sheet", HSSFWorkbook.ENCODING_UTF_16);
            //建立Excel title
           
            HSSFCellStyle style = excelDemo.createTitleStyle(workbook);
            HSSFRow rowTitle = sheet.createRow(0);
            for(int i=0;i<3;i++)
            {
                HSSFCell cellTitle = rowTitle.createCell((short)i);
                cellTitle.setCellStyle(style);
                cellTitle.setCellValue("The "+(i+1)+" Cell");
            }
            //建立Excel行,這裡建立兩行三列
            for(int i=1;i<3;i++)
            {
                HSSFRow row = sheet.createRow(i);
                for(int j=1;j<4;j++)
                {
                    HSSFCell cell = row.createCell((short)(j-1));
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                    cell.setCellStyle(style);
                    cell.setCellValue(values[i*j]);
                }
            }
            //sheet.createFreezePane(3, 2);//分割工作區
            //將產生的excel寫入
            String path = excelDemo.getClassPath();
            System.out.println("獲得的路徑是:"+path);
            FileOutputStream fileWrite = new FileOutputStream(path+"//"+"excelDemo.xls");
            workbook.write(fileWrite);
            fileWrite.flush();
            fileWrite.close();
            System.out.println("Excel 產生成功");
        }catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
    //獲得工程目錄路徑
    private String getClassPath()
    {
        return this.getClass().getClassLoader().getResource("").getPath();
    }
    //設定cell Style
    private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
        HSSFFont boldFont = wb.createFont();
        boldFont.setFontHeight((short) 200);
        HSSFCellStyle style = wb.createCellStyle();
        style.setFont(boldFont);
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        return style;
    }
    //設定Cell 寬度
    private void setColumnWidth(HSSFSheet sheet)
    {
        //根據你資料裡面的記錄有多少列,就設定多少列
        sheet.setColumnWidth((short) 0, (short) 3000);
        sheet.setColumnWidth((short) 1, (short) 3000);
        sheet.setColumnWidth((short) 2, (short) 3000);
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.