锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲中文字幕一二三四区,亚洲一区精彩视频,亚洲а∨精品天堂在线http://www.tkk7.com/brock/category/15415.htmlzh-cnWed, 28 Feb 2007 19:53:25 GMTWed, 28 Feb 2007 19:53:25 GMT60excel poi 澶勭悊2http://www.tkk7.com/brock/archive/2006/09/19/70548.htmlbrockbrockTue, 19 Sep 2006 07:59:00 GMThttp://www.tkk7.com/brock/archive/2006/09/19/70548.htmlhttp://www.tkk7.com/brock/comments/70548.htmlhttp://www.tkk7.com/brock/archive/2006/09/19/70548.html#Feedback0http://www.tkk7.com/brock/comments/commentRss/70548.htmlhttp://www.tkk7.com/brock/services/trackbacks/70548.html 鍦ㄥ鍑烘暟鎹敓鎴恊xcel鏃訛紝瀹氫箟excel鍗曞厓鏍兼牸寮忎負鏂囨湰銆?br />鏈濂芥嬁浣犵殑瀵煎嚭浠g爜璐村嚭鏉ョ湅鐪嬶紝涓昏鏄敓鎴恊xcel鏃朵唬鐮?br />
--------------------------------------------------------------------------------

鍙傝冧竴涓?
<% @聽page聽contentType = " text/html;聽charset=gb2312 " %>
<% @聽page聽 import = " java.io.* " %>
<% @聽page聽 import = " org.apache.poi.hssf.util.HSSFColor " %>
<% @聽page聽 import = " org.apache.poi.hssf.util.Region " %>
<% @聽page聽 import = " org.apache.poi.hssf.usermodel.* " %>
<%
// 鍒濆鍖?br /> // 宸ヤ綔綈?/span>
HSSFWorkbook聽wb = new 聽HSSFWorkbook();
// 宸ヤ綔琛?/span>
HSSFSheet聽sheet = wb.createSheet();
wb.setSheetName(
0 , " Excel婕旂ず! " ,HSSFWorkbook.ENCODING_UTF_16);
// 鍑嗗瀹屾垚
// 寤烘牱寮?br /> // 鏍囬瀛?/span>
HSSFFont聽font_Header = wb.createFont();
font_Header.setFontName(
" headerFont " );
font_Header.setFontHeightInPoints((
short ) 12 );
HSSFCellStyle聽cellStyle_Header
= wb.createCellStyle();
cellStyle_Header.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle_Header.setFont(font_Header);
// 閫氱敤琛?/span>
HSSFCellStyle聽cellStyle_Normal = wb.createCellStyle();
cellStyle_Normal.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// cellStyle_Normal.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// cellStyle_Normal.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// cellStyle_Normal.setBorderRight(HSSFCellStyle.BORDER_THIN);
// cellStyle_Normal.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 琛ㄦ牸澶?/span>
HSSFCellStyle聽cellStyle_Column = wb.createCellStyle();
cellStyle_Column.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle_Column.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle_Column.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle_Column.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle_Column.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle_Column.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle_Column.setFillPattern(HSSFCellStyle.BIG_SPOTS);
cellStyle_Column.setFillBackgroundColor((
short )HSSFColor.WHITE.index);
cellStyle_Column.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
// 鏁版嵁琛?/span>
HSSFCellStyle聽cellStyle_Cell = wb.createCellStyle();
cellStyle_Cell.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cellStyle_Cell.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle_Cell.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle_Cell.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle_Cell.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle_Cell.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle_Cell.setWrapText(
true );
// 鏍峰紡緇撴潫
// 緗爣棰?/span>
HSSFRow聽row = sheet.createRow(( short ) 0 );
HSSFCell聽cell
= row.createCell(( short ) 0 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" Excel婕旂ず! " );
sheet.addMergedRegion(
new 聽Region( 0 ,( short ) 0 , 0 ,( short ) 5 ));
cell.setCellStyle(cellStyle_Header);
// 瀹屾垚鏍囬
// 琛屼俊鎭?/span>
row = sheet.createRow(( short ) 1 );
cell
= row.createCell(( short ) 0 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" FieldName " );
cell.setCellStyle(cellStyle_Normal);
cell
= row.createCell(( short ) 1 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" FieldValue " );
sheet.addMergedRegion(
new 聽Region( 1 ,( short ) 1 , 1 ,( short ) 2 ));
cell.setCellStyle(cellStyle_Normal);
cell
= row.createCell(( short ) 3 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" FieldName " );
cell.setCellStyle(cellStyle_Normal);
cell
= row.createCell(( short ) 4 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" FieldValue " );
sheet.addMergedRegion(
new 聽Region( 1 ,( short ) 4 , 1 ,( short ) 5 ));
cell.setCellStyle(cellStyle_Normal);
// 琛ㄦ暟鎹?/span>
for ( int 聽mIndex = 2 ;mIndex < 10 ;mIndex ++ )
{
row
= sheet.createRow(( short )mIndex);
for ( int 聽nIndex = 0 ;nIndex < 6 ;nIndex ++ )
{
cell
= row.createCell(( short )nIndex);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" 00.00 " );
cell.setCellStyle(cellStyle_Cell);
}

}

// 鍏ㄥ眬璋?/span>
sheet.setHorizontallyCenter( true );
for ( int 聽kIndex = 0 ;kIndex < 10 ;kIndex ++ )
{
sheet.setColumnWidth((
short )kIndex,( short ) 4500 );
}

sheet.setMargin(HSSFSheet.BottomMargin,(
double ) 0.5 );
sheet.setMargin(HSSFSheet.LeftMargin,(
double ) 0.1 );
sheet.setMargin(HSSFSheet.RightMargin,(
double ) 0.1 );
sheet.setMargin(HSSFSheet.TopMargin,(
double ) 0.5 );
// 璋冩暣緇撴潫
// 杈撳嚭Excel
OutputStream聽outData = null ;
outData
= response.getOutputStream();
response.setContentType(
" application/vnd.ms-excel " );
wb.write(outData);
outData.flush();
response.flushBuffer();
// 瀹屾垚
%>

brock 2006-09-19 15:59 鍙戣〃璇勮
]]>
Busy Developers' Guide to HSSF Features (璇存槑涔?http://www.tkk7.com/brock/archive/2006/09/14/69688.htmlbrockbrockThu, 14 Sep 2006 08:47:00 GMThttp://www.tkk7.com/brock/archive/2006/09/14/69688.htmlhttp://www.tkk7.com/brock/comments/69688.htmlhttp://www.tkk7.com/brock/archive/2006/09/14/69688.html#Feedback2http://www.tkk7.com/brock/comments/commentRss/69688.htmlhttp://www.tkk7.com/brock/services/trackbacks/69688.html

Features

New Workbook

    //鍒涘緩鏂扮殑Excel 宸ヤ綔綈?br />聽聽聽 HSSFWorkbook wb = new HSSFWorkbook();
//new 涓涓狥ileOutputStream FileOutputStream fileOut = new FileOutputStream("workbook.xls");
聽聽聽聽//鍐欏叆嫻? wb.write(fileOut); fileOut.close();

New Sheet

    HSSFWorkbook wb = new HSSFWorkbook();
銆 // 濡傝鏂板緩涓鍚嶄負"鏁堢泭鎸囨爣"鐨勫伐浣滆〃錛屽叾璇彞涓猴細
銆// HSSFSheet sheet = workbook.createSheet("鏁堢泭鎸囨爣");
HSSFSheet sheet1 = wb.createSheet("new sheet"); HSSFSheet sheet2 = wb.createSheet("second sheet"); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close();

Creating Cells

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.

聽聽聽聽聽//鍒涘緩涓琛屽茍鏀句竴浜涘厓绱犲?,0鏄搗濮嬭 HSSFRow row = sheet.createRow((short)0); // Create a cell and put a value in it.
鍒涘緩涓涓崟鍏冩牸 HSSFCell cell = row.createCell((short)0); cell.setCellValue(1); // Or do it on one line.
鍒涘緩鍗曞厓鏍間篃鍙互涓琛屼唬鐮? row.createCell((short)1).setCellValue(1.2); row.createCell((short)2).setCellValue("This is a string"); row.createCell((short)3).setCellValue(true); // Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close();

Creating Date Cells

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short)0);

    // Create a cell and put a date value in it.  The first cell is not styled
    // as a date.
    HSSFCell cell = row.createCell((short)0);
    cell.setCellValue(new Date());

    // we style the second cell as a date (and time).  It is important to
    // create a new cell style from the workbook otherwise you can end up
    // modifying the built in style and effecting not only this cell but other cells.
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    cell = row.createCell((short)1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Working with different types of cells涓嶅悓綾誨瀷鐨勫崟鍏冩牸涓璧峰伐浣?/h2>

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow((short)2);
    row.createCell((short) 0).setCellValue(1.1);
    row.createCell((short) 1).setCellValue(new Date());
    row.createCell((short) 2).setCellValue("a string");
    row.createCell((short) 3).setCellValue(true);
    row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Demonstrates various alignment options紺鴻寖涓嶅悓鐨勫榻愰夐」

    public static void main(String[] args)
            throws IOException
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row = sheet.createRow((short) 2);
        createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
        createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
        createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
        createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
        createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
        createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
        createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();

    }

    /**
     * Creates a cell and aligns it a certain way.
     *
     * @param wb        the workbook
     * @param row       the row to create the cell in
     * @param column    the column number to create the cell in
     * @param align     the alignment for the cell.
     */
    private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
    {
        HSSFCell cell = row.createCell(column);
        cell.setCellValue("Align It");
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(align);
        cell.setCellStyle(cellStyle);
    }
                    

Working with borders璁劇疆杈規(guī)

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);

    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue(4);

    // Style the cell with borders all around.
    HSSFCellStyle style = wb.createCellStyle();
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBottomBorderColor(HSSFColor.BLACK.index);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setLeftBorderColor(HSSFColor.GREEN.index);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setRightBorderColor(HSSFColor.BLUE.index);
    style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
    style.setTopBorderColor(HSSFColor.BLACK.index);
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Fills and colors濉厖 鍜?棰滆壊

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);

    // Aqua background
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(HSSFColor.AQUA.index);
    style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("X");
    cell.setCellStyle(style);

    // Orange "foreground", foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.ORANGE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cell = row.createCell((short) 2);
    cell.setCellValue("X");
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Merging cells 鍚堝茍鍗曞厓鏍?/h2>

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    HSSFRow row = sheet.createRow((short) 1);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of merging");

    sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Working with fonts 瀛椾綋

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);

    // Create a new font and alter it.
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short)24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);

    // Fonts are set into a style so create a new one to use.
    HSSFCellStyle style = wb.createCellStyle();
    style.setFont(font);

    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of fonts");
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Custom colors 鑷畾涔夐鑹?/h2>

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    HSSFRow row = sheet.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("Default Palette");

    //apply some colors from the standard palette,
    // as in the previous examples.
    //we'll use red text on a lime background

    HSSFCellStyle style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.LIME.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFFont font = wb.createFont();
    font.setColor(HSSFColor.RED.index);
    style.setFont(font);

    cell.setCellStyle(style);

    //save with the default palette
    FileOutputStream out = new FileOutputStream("default_palette.xls");
    wb.write(out);
    out.close();

    //now, let's replace RED and LIME in the palette
    // with a more attractive combination
    // (lovingly borrowed from freebsd.org)

    cell.setCellValue("Modified Palette");

    //creating a custom palette for the workbook
    HSSFPalette palette = wb.getCustomPalette();

    //replacing the standard red with freebsd.org red
    palette.setColorAtIndex(HSSFColor.RED.index,
            (byte) 153,  //RGB red (0-255)
            (byte) 0,    //RGB green
            (byte) 0     //RGB blue
    );
    //replacing lime with freebsd.org gold
    palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);

    //save with the modified palette
    // note that wherever we have previously used RED or LIME, the
    // new colors magically appear
    out = new FileOutputStream("modified_palette.xls");
    wb.write(out);
    out.close();
                    

Reading and Rewriting Workbooks聽 璇誨拰 閲嶅啓宸ヤ綔綈?/h2>

    POIFSFileSystem fs      =
            new POIFSFileSystem(new FileInputStream("workbook.xls"));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = sheet.getRow(2);
    HSSFCell cell = row.getCell((short)3);
    if (cell == null)
        cell = row.createCell((short)3);
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellValue("a test");

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Using newlines in cells聽 鎹㈣

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFRow r = null;
    HSSFCell c = null;
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFFont f = wb.createFont();
    HSSFFont f2 = wb.createFont();

    cs = wb.createCellStyle();

    cs.setFont( f2 );
    //Word Wrap MUST be turned on
    cs.setWrapText( true );

    r = s.createRow( (short) 2 );
    r.setHeight( (short) 0x349 );
    c = r.createCell( (short) 2 );
    c.setCellType( HSSFCell.CELL_TYPE_STRING );
    c.setCellValue( "Use \n with word wrap on to create a new line" );
    c.setCellStyle( cs );
    s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );

    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();

Data Formats聽 鏁版嵁鏍煎紡

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFCellStyle style;
    HSSFDataFormat format = wb.createDataFormat();
    HSSFRow row;
    HSSFCell cell;
    short rowNum = 0;
    short colNum = 0;

    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("0.0"));
    cell.setCellStyle(style);

    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("#,##0.0000"));
    cell.setCellStyle(style);

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Fit Sheet to One Page聽聽 涓欏典腑鏄捐鍚堥傜殑宸ヤ綔琛?/h2>

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFPrintSetup ps = sheet.getPrintSetup();
    
    sheet.setAutobreaks(true);
    
    ps.setFitHeight((short)1);
    ps.setFitWidth((short)1);


    // Create various cells and rows for spreadsheet.

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Set Print Area 璁劇疆鎵撳嵃鍖?/h2>

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    wb.setPrintArea(0, "$A$1:$C$2");
    //sets the print area for the first sheet
    //Alternatively:
    //wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)  

    // Create various cells and rows for spreadsheet.

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    
    
                    

Set Page Numbers on Footer 璁劇疆欏墊暟 鍦ㄩ〉鐢ㄤ腑

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFFooter footer = sheet.getFooter()
    
    footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
    


    // Create various cells and rows for spreadsheet.

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Using the Convenience Functions聽 琛岀敤鏂逛究鐨勬柟娉?/h2>

The convenience functions live in contrib and provide utility features such as setting borders around merged regions and changing style attributes without explicitly creating new styles.

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet( "new sheet" );

    // Create a merged region
    HSSFRow row = sheet1.createRow( (short) 1 );
    HSSFRow row2 = sheet1.createRow( (short) 2 );
    HSSFCell cell = row.createCell( (short) 1 );
    cell.setCellValue( "This is a test of merging" );
    Region region = new Region( 1, (short) 1, 4, (short) 4 );
    sheet1.addMergedRegion( region );

    // Set the border and border colors.
    final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
    HSSFRegionUtil.setBorderBottom( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderTop( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderLeft( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderRight( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);

    // Shows some usages of HSSFCellUtil
    HSSFCellStyle style = wb.createCellStyle();
    style.setIndention((short)4);
    HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
    HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
    HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);

    // Write out the workbook
    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();
                    

Shift rows up or down on a sheet

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("row sheet");

    // Create various cells and rows for spreadsheet.

    // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
    sheet.shiftRows(5, 10, -5);

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Set a sheet as selected 璁劇疆涓轟互閫?/h2>

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("row sheet");
    sheet.setSelected(true);

    // Create various cells and rows for spreadsheet.

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Set the zoom magnification璁劇疆鏀懼ぇ鐜?/h2>

The zoom is expressed as a fraction. For example to express a zoom of 75% use 3 for the numerator and 4 for the denominator.

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    sheet1.setZoom(3,4);   // 75 percent magnification
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Splits and freeze panes

There are two types of panes you can create; freeze panes and split panes.

A freeze pane is split by columns and rows. You create a freeze pane using the following mechanism:

sheet1.createFreezePane( 3, 2, 3, 2 );

The first two parameters are the columns and rows you wish to split by. The second two parameters indicate the cells that are visible in the bottom right quadrant.

Split pains appear differently. The split area is divided into four separate work area's. The split occurs at the pixel level and the user is able to adjust the split by dragging it to a new position.

Split panes are created with the following call:

sheet2.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT );

The first parameter is the x position of the split. This is in 1/20th of a point. A point in this case seems to equate to a pixel. The second parameter is the y position of the split. Again in 1/20th of a point.

The last parameter indicates which pane currently has the focus. This will be one of HSSFSheet.PANE_LOWER_LEFT, PANE_LOWER_RIGHT, PANE_UPPER_RIGHT or PANE_UPPER_LEFT.

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");
    HSSFSheet sheet3 = wb.createSheet("third sheet");
    HSSFSheet sheet4 = wb.createSheet("fourth sheet");

    // Freeze just one row
    sheet1.createFreezePane( 0, 1, 0, 1 );
    // Freeze just one column
    sheet2.createFreezePane( 1, 0, 1, 0 );
    // Freeze the columns and rows (forget about scrolling position of the lower right quadrant).
    sheet3.createFreezePane( 2, 2 );
    // Create a split with the lower left side being the active quadrant
    sheet4.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT );

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Repeating rows and columns

It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the HSSFWorkbook class.

This function Contains 5 parameters. The first parameter is the index to the sheet (0 = first sheet). The second and third parameters specify the range for the columns to repreat. To stop the columns from repeating pass in -1 as the start and end column. The fourth and fifth parameters specify the range for the rows to repeat. To stop the columns from repeating pass in -1 as the start and end rows.

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");

    // Set the columns to repeat from column 0 to 2 on the first sheet
    wb.setRepeatingRowsAndColumns(0,0,2,-1,-1);
    // Set the the repeating rows and columns on the second sheet.
    wb.setRepeatingRowsAndColumns(1,4,5,1,2);

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Headers and Footers 欏靛ご 鍜?欏佃剼

Example is for headers but applies directly to footers.

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    HSSFHeader header = sheet.getHeader();
    header.setCenter("Center Header");
    header.setLeft("Left Header");
    header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") + 
                    HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    

Drawing Shapes

POI supports drawing shapes using the Microsoft Office drawing tools. Shapes on a sheet are organized in a hiearchy of groups and and shapes. The top-most shape is the patriarch. This is not visisble on the sheet at all. To start drawing you need to call createPatriarch on the HSSFSheet class. This has the effect erasing any other shape information stored in that sheet. By default POI will leave shape records alone in the sheet unless you make a call to this method.

To create a shape you have to go through the following steps:

  1. Create the patriarch.
  2. Create an anchor to position the shape on the sheet.
  3. Ask the patriarch to create the shape.
  4. Set the shape type (line, oval, rectangle etc...)
  5. Set any other style details converning the shape. (eg: line thickness, etc...)
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
    HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
                    

Text boxes are created using a different call:

    HSSFTextbox textbox1 = patriarch.createTextbox(
            new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)2,2));
    textbox1.setString(new HSSFRichTextString("This is a test") );
                    

It's possible to use different fonts to style parts of the text in the textbox. Here's how:

    HSSFFont font = wb.createFont();
    font.setItalic(true);
    font.setUnderline(HSSFFont.U_DOUBLE);
    HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
    string.applyFont(2,5,font);
    textbox.setString(string );
                    

Just as can be done manually using Excel, it is possible to group shapes together. This is done by calling createGroup() and then creating the shapes using those groups.

It's also possible to create groups within groups.

Warning
Any group you create should contain at least two other shapes or subgroups.

Here's how to create a shape group:

    // Create a shape group.
    HSSFShapeGroup group = patriarch.createGroup(
            new HSSFClientAnchor(0,0,900,200,(short)2,2,(short)2,2));

    // Create a couple of lines in the group.
    HSSFSimpleShape shape1 = group.createShape(new HSSFChildAnchor(3,3,500,500));
    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
    ( (HSSFChildAnchor) shape1.getAnchor() ).setAnchor((short)3,3,500,500);
    HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor((short)1,200,400,600));
    shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
                    

If you're being observant you'll noticed that the shapes that are added to the group use a new type of anchor: the HSSFChildAnchor. What happens is that the created group has it's own coordinate space for shapes that are placed into it. POI defaults this to (0,0,1023,255) but you are able to change it as desired. Here's how:

    myGroup.setCoordinates(10,10,20,20); // top-left, bottom-right
                    

If you create a group within a group it's also going to have it's own coordinate space.

Styling Shapes聽

By default shapes can look a little plain. It's possible to apply different styles to the shapes however. The sorts of things that can currently be done are:

  • Change the fill color.
  • Make a shape with no fill color.
  • Change the thickness of the lines.
  • Change the style of the lines. Eg: dashed, dotted.
  • Change the line color.

Here's an examples of how this is done:

    HSSFSimpleShape s = patriarch.createSimpleShape(a);
    s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
    s.setLineStyleColor(10,10,10);
    s.setFillColor(90,10,200);
    s.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3);
    s.setLineStyle(HSSFShape.LINESTYLE_DOTSYS);
                    

Shapes and Graphics2d

While the native POI shape drawing commands are the recommended way to draw shapes in a shape it's sometimes desirable to use a standard API for compatibility with external libraries. With this in mind we created some wrappers for Graphics and Graphics2d.

Warning
It's important to not however before continuing that Graphics2d is a poor match to the capabilities of the Microsoft Office drawing commands. The older Graphics class offers a closer match but is still a square peg in a round hole.

All Graphics commands are issued into an HSSFShapeGroup. Here's how it's done:

    a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
    group = patriarch.createGroup( a );
    group.setCoordinates( 0, 0, 80 * 4 , 12 * 23  );
    float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1());
    g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
    g2d = new EscherGraphics2d( g );
    drawChemicalStructure( g2d );
                    

The first thing we do is create the group and set it's coordinates to match what we plan to draw. Next we calculate a reasonable fontSizeMultipler then create the EscherGraphics object. Since what we really want is a Graphics2d object we create an EscherGraphics2d object and pass in the graphics object we created. Finally we call a routine that draws into the EscherGraphics2d object.

The vertical points per pixel deserves some more explanation. One of the difficulties in converting Graphics calls into escher drawing calls is that Excel does not have the concept of absolute pixel positions. It measures it's cell widths in 'characters' and the cell heights in points. Unfortunately it's not defined exactly what type of character it's measuring. Presumably this is due to the fact that the Excel will be using different fonts on different platforms or even within the same platform.

Because of this constraint we've had to implement the concept of a verticalPointsPerPixel. This the amount the font should be scaled by when you issue commands such as drawString(). To calculate this value use the follow formula:

    multipler = groupHeightInPoints / heightOfGroup
                    

The height of the group is calculated fairly simply by calculating the difference between the y coordinates of the bounding box of the shape. The height of the group can be calculated by using a convenience called HSSFClientAnchor.getAnchorHeightInPoints().

Many of the functions supported by the graphics classes are not complete. Here's some of the functions that are known to work.

  • fillRect()
  • fillOval()
  • drawString()
  • drawOval()
  • drawLine()
  • clearRect()

Functions that are not supported will return and log a message using the POI logging infrastructure (disabled by default).

Outlining

Outlines are great for grouping sections of information together and can be added easily to columns and rows using the POI API. Here's how:

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupRow( 5, 14 );
    sheet1.groupRow( 7, 14 );
    sheet1.groupRow( 16, 19 );

    sheet1.groupColumn( (short)4, (short)7 );
    sheet1.groupColumn( (short)9, (short)12 );
    sheet1.groupColumn( (short)10, (short)11 );

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
                    

To collapse (or expand) an outline use the following calls:

    sheet1.setRowGroupCollapsed( 7, true );
    sheet1.setColumnGroupCollapsed( (short)4, true );
                    

The row/column you choose should contain an already created group. It can be anywhere within the group.

Images

Images are part of the drawing support. To add an image just call createPicture() on the drawing patriarch. At the time of writing the following types are supported:

  • PNG
  • JPG
  • DIB

It is not currently possible to read existing images and it should be noted that any existing drawings may be erased once you add a image to a sheet.

    // Create the drawing patriarch.  This is the top level container for
    // all shapes. This will clear out any existing shapes for that sheet.
    HSSFPatriarch patriarch = sheet5.createDrawingPatriarch();

    HSSFClientAnchor anchor;
    anchor = new HSSFClientAnchor(0,0,0,255,(short)2,2,(short)4,7);
    anchor.setAnchorType( 2 );
    patriarch.createPicture(anchor, loadPicture( "src/resources/logos/logoKarmokar4.png", wb ));
            

Named Ranges and Named Cells

Named Range is a way to refer to a group of cells by a name. Named Cell is a degenerate case of Named Range in that the 'group of cells' contains exactly one cell. You can create as well as refer to cells in a workbook by their named range. When working with Named Ranges, the classes: org.apache.poi.hssf.util.CellReference and & org.apache.poi.hssf.util.AreaReference are used.

Creating Named Range / Named Cell

    // setup code
    String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(sname);
    sheet.createRow(0).createCell((short) 0).setCellValue(cvalue);
     
    // 1. create named range for a single cell using areareference
    HSSFName namedCell = wb.createName();
    namedCell.setNameName(cname);
    String reference = sname+"!A1:A1"; // area reference
    namedCell.setReference(reference);
    
    // 2. create named range for a single cell using cellreference
    HSSFName namedCell = wb.createName();
    namedCell.setNameName(cname);
    String reference = sname+"!A1"; // cell reference
    namedCell.setReference(reference);
    
    // 3. create named range for an area using AreaReference
    HSSFName namedCell = wb.createName();
    namedCell.setNameName(cname);
    String reference = sname+"!A1:C5"; // area reference
    namedCell.setReference(reference);
    
            

Reading from Named Range / Named Cell

    // setup code
    String cname = "TestName";
    HSSFWorkbook wb = getMyWorkbook(); // retrieve workbook

    // retrieve the named range
    int namedCellIdx = wb.getNameIndex(cellName);
    HSSFName aNamedCell = wb.getNameAt(namedCellIdx);
    
    // retrieve the cell at the named range and test its contents
    AreaReference aref = new AreaReference(aNamedCell.getReference());
    CellReference[] crefs = aref.getCells();
    for (int i=0; i<crefs.length; i++) {
        HSSFSheet s = wb.getSheet(crefs[i].getSheetName());
        HSSFRow r = sheet.getRow(crefs[i].getRow());
        HSSFCell c = r.getCell(crefs[i].getCol());
        // extract the cell contents based on cell type etc.
    }
            


brock 2006-09-14 16:47 鍙戣〃璇勮
]]>
excel 澶勭悊 Poihttp://www.tkk7.com/brock/archive/2006/09/14/69674.htmlbrockbrockThu, 14 Sep 2006 08:24:00 GMThttp://www.tkk7.com/brock/archive/2006/09/14/69674.htmlhttp://www.tkk7.com/brock/comments/69674.htmlhttp://www.tkk7.com/brock/archive/2006/09/14/69674.html#Feedback0http://www.tkk7.com/brock/comments/commentRss/69674.htmlhttp://www.tkk7.com/brock/services/trackbacks/69674.htmlexcel 鏍峰紡

// create聽a聽cell聽object聽 鍒涘緩涓涓厓绱?琛岀殑鍒?瀵硅薄
HSSFCell聽cell聽 = 聽row.createCell(column);
// create聽a聽cell聽style聽object 鍒涘緩涓涓厓绱犵殑鏍峰紡瀵硅薄
HSSFCellStyle聽cellStyle聽 = 聽wb.createCellStyle();
cellStyle.setAlignment(align);聽
cellStyle.setVerticalAlignment(valign);
// set聽cell聽border聽聽 璁劇疆鍏冪礌鐨勮竟妗?/span>
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
// set聽foreground聽color 璁劇疆鍏冪礌鐨勫墠鏅壊
cellStyle.setFillForegroundColor(bgColor);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// set聽font 璁劇疆瀛椾綋
cellStyle.setFont(font);
// set聽size
// set聽the聽style聽of聽this聽cell聽 鎶婃牱寮忓姞鍒板厓绱犱腑
cell.setCellStyle(cellStyle);
// set聽cell&#39;s聽charset 璁劇疆瀛楃 (涓枃闂)
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
// set聽cell聽value 鏀瑰厓绱犺祴鍊?/span>
cell.setCellValue(value);
浠巇ata聽 涓緱鍒版暟鎹?
ArrayList聽datacell_list聽=聽DBToExcel.getSheetDataCol(tableid,k_row,new聽DBAgent());
for(int聽k_col=0;k_col<datacell_list.size();k_col++){
銆銆HSSFCell聽cell聽
=聽row.createCell((short)k_col);
銆銆cell.setEncoding(HSSFCell.ENCODING_UTF_16);

銆銆SheetDataBean聽sdb聽
=(SheetDataBean)datacell_list.get(k_col);
colnum聽
=聽sdb.getColnum();

String聽data聽
=聽DBToExcel.getSheetCellValue(tableid,k_row,colnum,new聽DBAgent());
//cell.setCellValue(data);銆
鎵撳嵃
1.
HSSFCell聽c;
..
c.setEncoding(HSSFCell.ENCODING_UTF_16);
c.setCellValue(
"嫻嬭瘯嫻嬭瘯嫻嬭瘯嫻嬭瘯嫻嬭瘯嫻嬭瘯嫻嬭瘯嫻嬭瘯");

2.
鎵撳嵃璁劇疆
import聽org.apache.poi.hssf.usermodel.HSSFPrintSetup;
鍒涘緩鎵撳嵃璁劇疆瀵硅薄
HSSFPrintSetup聽hps聽
=聽hs.getPrintSetup();
璁劇疆A4綰?br />hps.setPaperSize((
short)9);聽
灝嗛〉闈㈣緗負妯悜鎵撳嵃妯″紡
hps.setLandscape(
true);聽
璁劇疆鎵撳嵃欏甸潰涓烘按騫沖眳涓?br />sheet.setHorizontallyCenter(
true);聽
璁劇疆鎵撳嵃欏甸潰涓哄瀭鐩村眳涓?br />sheet.setVerticallyCenter(
true);


brock 2006-09-14 16:24 鍙戣〃璇勮
]]>
鍒╃敤POI灝嗘暟鎹〃瀵煎叆Excel http://www.tkk7.com/brock/archive/2006/07/26/60186.htmlbrockbrockWed, 26 Jul 2006 07:30:00 GMThttp://www.tkk7.com/brock/archive/2006/07/26/60186.htmlhttp://www.tkk7.com/brock/comments/60186.htmlhttp://www.tkk7.com/brock/archive/2006/07/26/60186.html#Feedback0http://www.tkk7.com/brock/comments/commentRss/60186.htmlhttp://www.tkk7.com/brock/services/trackbacks/60186.html
  • /*
  • 聽*聽QuickExcel.java
  • 聽*聽浣滆咃細鏉ㄥ簡鎴?/font>
  • 聽*聽Created聽on聽2004騫?1鏈?2鏃?聽涓嬪崍4:05
  • 聽*聽鍦ㄥ疄闄呭簲鐢ㄤ腑緇忓父瑕佸皢鏁版嵁搴撲腑鐨勮〃瀵煎叆Excel
  • 聽*聽鏈漢鍦ˋpache鐨凱OI鍩虹涓婂啓浜嗕竴涓畝鍗曠殑綾?/font>
  • 聽*聽鏈変笉褰撴寚鍑鴻鎸囨錛岃阿璋紒
  • 聽*聽
  • 聽*/
  • package 聽yqc.poi;
  • import 聽java.sql.*;
  • import 聽java.util.*;
  • import 聽java.io.*;
  • import 聽java.io.ByteArrayInputStream;
  • import 聽java.io.FileInputStream;
  • import 聽java.io.FileOutputStream;
  • import 聽java.io.IOException;
  • import 聽org.apache.poi.hssf.usermodel.*;
  • import 聽org.apache.poi.poifs.filesystem.POIFSFileSystem;
  • import 聽org.apache.poi.hssf.record.*;
  • import 聽org.apache.poi.hssf.model.*;
  • import 聽org.apache.poi.hssf.usermodel.*;
  • import 聽org.apache.poi.hssf.util.*;import聽yqc.sql.*;
  • /**
  • 聽*
  • 聽*聽@author聽聽Administrator
  • 聽*/
  • public class聽QuickExcel聽{
  • 聽聽聽聽
  • 聽聽聽聽/**聽Creates聽a聽new聽instance聽of聽QuickExcel聽*/
  • 聽聽聽聽private聽QuickExcel(String聽file){
  • 聽聽聽聽聽聽聽聽_file=file;
  • 聽聽聽聽}
  • 聽聽聽聽
  • 聽聽聽聽privatevoid聽open()throwsIOException{
  • 聽聽聽聽聽聽聽聽InputStream聽stream聽=聽null;
  • 聽聽聽聽聽聽聽聽Record[]聽records聽=聽null;
  • 聽聽聽聽聽聽聽聽POIFSFileSystem聽fs聽=
  • 聽聽聽聽聽聽聽聽聽聽聽聽new聽POIFSFileSystem(newFileInputStream(_file));
  • 聽聽聽聽聽聽聽聽_wb聽=聽new聽HSSFWorkbook(fs);
  • 聽聽聽聽}
  • 聽聽聽聽
  • 聽聽聽聽privatevoid聽create(){
  • 聽聽聽聽聽聽聽聽_wb=new聽HSSFWorkbook();
  • 聽聽聽聽}
  • 聽聽聽聽
  • 聽聽聽聽publicstatic聽QuickExcel聽newInstance聽(String聽file){
  • 聽聽聽聽聽聽聽聽QuickExcel聽qe=new聽QuickExcel(file);
  • 聽聽聽聽聽聽聽聽qe.create();
  • 聽聽聽聽聽聽聽聽return聽qe;
  • 聽聽聽聽}
  • 聽聽聽聽
  • 聽聽聽聽publicstatic聽QuickExcel聽openInstance(String聽file)聽throwsIOException聽{
  • 聽聽聽聽聽聽聽聽QuickExcel聽qe=new聽QuickExcel(file);
  • 聽聽聽聽聽聽聽聽qe.open();
  • 聽聽聽聽聽聽聽聽return聽qe;
  • 聽聽聽聽}
  • 聽聽聽聽
  • 聽聽聽聽publicvoid聽close(){
  • 聽聽聽聽聽聽聽聽try{
  • 聽聽聽聽聽聽聽聽聽聽聽聽FileOutputStream聽fileOut聽=聽newFileOutputStream(_file);
  • 聽聽聽聽聽聽聽聽聽聽聽聽_wb.write(fileOut);//鎶奧orkbook瀵硅薄杈撳嚭鍒版枃浠秝orkbook.xls涓?/font>
  • 聽聽聽聽聽聽聽聽聽聽聽聽fileOut.close();
  • 聽聽聽聽聽聽聽聽}
  • 聽聽聽聽聽聽聽聽catch聽(Exception聽ex){
  • 聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(ex.getMessage());
  • 聽聽聽聽聽聽聽聽}
  • 聽聽聽聽}
  • 聽聽聽聽
  • 聽聽聽聽privatevoid聽removeSheet(String聽sheetName){
  • 聽聽聽聽聽聽聽聽int聽i=_wb.getSheetIndex("sheetName");
  • 聽聽聽聽聽聽聽聽if聽(i>=0)聽_wb.removeSheetAt(i);
  • 聽聽聽聽}
  • 聽聽聽聽
  • 聽聽聽聽publicint聽fillSheet聽(ResultSet聽rs,String聽sheetName)throwsSQLException聽{
  • 聽聽聽聽聽聽聽聽HSSFSheet聽st=聽_wb.createSheet(sheetName);
  • 聽聽聽聽聽聽聽聽ResultSetMetaData聽rsmd=聽rs.getMetaData();
  • 聽聽聽聽聽聽聽聽int聽index=0;
  • 聽聽聽聽聽聽聽聽int聽result=0;
  • 聽聽聽聽聽聽聽聽HSSFRow聽row=st.createRow(index++);
  • 聽聽聽聽聽聽聽聽for(int聽i=1;i<=rsmd.getColumnCount();++i){
  • 聽聽聽聽聽聽聽聽聽聽聽聽HSSFCell聽cell=row.createCell((short)(i-1));
  • 聽聽聽聽聽聽聽聽聽聽聽聽cell.setCellValue(rsmd.getColumnName(i));
  • 聽聽聽聽聽聽聽聽}
  • 聽聽聽聽聽聽聽聽while(rs.next())聽{
  • 聽聽聽聽聽聽聽聽聽聽聽聽result++;
  • 聽聽聽聽聽聽聽聽聽聽聽聽row=st.createRow(index++);
  • 聽聽聽聽聽聽聽聽聽聽聽聽for(int聽i=1;i<=rsmd.getColumnCount();++i){
  • 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽HSSFCell聽cell=row.createCell((short)(i-1));
  • 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cell.setEncoding(cell.ENCODING_UTF_16);
  • 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cell.setCellValue(rs.getString(i));
  • 聽聽聽聽聽聽聽聽聽聽聽聽}
  • 聽聽聽聽聽聽聽聽}
  • 聽聽聽聽聽聽聽聽return聽result;
  • }
  • 聽聽聽聽
  • 聽聽聽聽publicstaticvoid聽main(String[]聽args){
  • 聽聽聽聽聽聽聽聽try{
  • 聽聽聽聽聽聽聽聽聽聽聽聽QuickConnection聽qc=new聽MssqlConnection("jdbc:microsoft:sqlserver://192.168.0.100:1433;DatabaseName=ls");
  • 聽聽聽聽聽聽聽聽聽聽聽聽QuickExcel聽qe=QuickExcel.newInstance("a.xls");
  • 聽聽聽聽聽聽聽聽聽聽聽聽qc.connect();
  • 聽聽聽聽聽聽聽聽聽聽聽聽String聽sql="select聽*聽from聽ls.dbo.radio1_emcee";
  • 聽聽聽聽聽聽聽聽聽聽聽聽ResultSet聽rs=qc.getStatement().executeQuery(sql);
  • 聽聽聽聽聽聽聽聽聽聽聽聽qe.fillSheet(rs,"MT");
  • 聽聽聽聽聽聽聽聽聽聽聽聽qe.close();
  • 聽聽聽聽聽聽聽聽聽聽聽聽qe=QuickExcel.openInstance("a.xls");
  • 聽聽聽聽聽聽聽聽聽聽聽聽qe.fillSheet(rs,"MO");
  • 聽聽聽聽聽聽聽聽聽聽聽聽qe.close();
  • 聽聽聽聽聽聽聽聽聽聽聽聽qc.close();
  • 聽聽聽聽聽聽聽聽}
  • 聽聽聽聽聽聽聽聽catch聽(SQLException聽ex){
  • 聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(ex.getMessage());
  • 聽聽聽聽聽聽聽聽}
  • 聽聽聽聽聽聽聽聽catch聽(IOException聽ex){
  • 聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(ex.getMessage());
  • 聽聽聽聽聽聽聽聽}
  • 聽聽聽聽}
  • 聽聽聽聽
  • 聽聽聽聽HSSFWorkbook聽_wb;
  • 聽聽聽聽String聽_file="new.xls";
  • }


  • brock 2006-07-26 15:30 鍙戣〃璇勮
    ]]>
    綰痡ava鐨凟xcel瑙e喅鏂規(guī)http://www.tkk7.com/brock/archive/2006/07/26/60185.htmlbrockbrockWed, 26 Jul 2006 07:29:00 GMThttp://www.tkk7.com/brock/archive/2006/07/26/60185.htmlhttp://www.tkk7.com/brock/comments/60185.htmlhttp://www.tkk7.com/brock/archive/2006/07/26/60185.html#Feedback0http://www.tkk7.com/brock/comments/commentRss/60185.htmlhttp://www.tkk7.com/brock/services/trackbacks/60185.html

    鎽樿:

    Apache鐨凧akata欏圭洰鐨凱OI瀛愰」鐩紝鐩爣鏄鐞唎le2瀵硅薄銆傜洰鍓嶆瘮杈冩垚鐔熺殑鏄疕SSF鎺ュ彛錛屽鐞哅S Excel錛?7-2002錛夊璞?
    寰蔣鍦ㄦ闈㈢郴緇熶笂鐨勬垚鍔燂紝浠ゆ垜浠笉寰椾笉澶ч噺浣跨敤瀹冪殑鍔炲叕浜у搧錛屽錛歐ord錛孍xcel銆傛椂鑷充粖鏃ワ紝瀹冪殑婧愪唬鐮佷粛鐒朵笉鍏紑宸插皝閿佷簡鎴戜滑鐨勮繘涓姝ュ簲鐢ㄥ拰寮鍙戙傜劧鑰屽湪瑕佹眰鏇撮珮鐨勬湇鍔″櫒棰嗗煙錛屽井杞湰韜殑浜у搧縐繪鎬т笉濂斤紝聽聽
    鎬ц兘涓嶄匠銆傚湪鎴戜滑瀹為檯鐨勫紑鍙戜腑錛岃〃鐜板眰鐨勮В鍐蟲柟妗堣櫧鐒舵湁澶氭牱錛屼絾鏄疘e嫻忚鍣ㄥ凡鎴愪負鏈澶氫漢浣跨敤鐨勬祻瑙堝櫒錛屽洜涓哄ぇ瀹墮兘鐢╓indows銆傚湪浼佷笟鍔炲叕緋葷粺涓紝甯稿父鏈夊鎴瘋繖鏍峰瓙瑕佹眰錛氫綘瑕佹妸鎴戜滑鐨勬姤琛ㄧ洿鎺ョ敤Excel鎵撳紑銆傛垨鑰呮槸錛氭垜浠凡緇忎範(fàn)鎯敤Excel鎵撳嵃銆傝繖鏍峰瓙濡傛灉鐢?net寮鍙戞槸娌℃湁闂鐨勶紝浣嗘槸鏈塲2ee榪欎釜姣?net鏇存湁鍓嶉旂殑寮鏀懼紡鐨勫紑鍙戠幆澧冿紝闅鵑亾鎴戜負浜嗚В鍐蟲墦鍗扮殑瑕佹眰鍘誨彟鍐欏鎴風(fēng)鐨勬帶浠訛紵鎴栬呭湪鏈嶅姟鍣ㄧ浣跨敤鏈湴浠g爜錛熺涓縐嶆柟妗堢殑闂鏄叧閿暟鎹殑澶勭悊鏈夋椂鍊欎笉鑳藉湪瀹㈡埛绔仛錛岀2縐嶆柟妗堢殑闂鏄壓鐗蹭簡浠g爜鐨勫彲縐繪鎬у拰紼沖畾鎬с傚鏋滆瀹㈡埛绔彧璐熻矗澶勭悊鐢熸垚濂界殑鎶ヨ〃錛岄偅灝嗘槸涓縐嶈浜虹殑閫夋嫨銆?br />
    Apache鐨凧akata欏圭洰鐨凱OI瀛愰」鐩紝鐩爣鏄鐞唎le2瀵硅薄銆傜洰鍓嶆瘮杈冩垚鐔熺殑鏄疕SSF鎺ュ彛錛屽鐞哅S Excel錛?7-2002錛夊璞°傚畠涓嶈薄鎴戜滑浠呬粎鏄敤csv鐢熸垚鐨勬病鏈夋牸寮忕殑鍙互鐢盓xcel杞崲鐨勪笢瑗匡紝鑰屾槸鐪熸鐨凟xcel瀵硅薄錛屼綘鍙互鎺у埗涓浜涘睘鎬уsheet,cell絳夌瓑銆傝繖鏄竴涓勾杞葷殑欏圭洰錛屾墍浠ヨ薄HDF榪欐牱鐩存帴鏀寔Word瀵硅薄鐨勫ソ涓滆タ浠嶇劧鍦ㄨ璁′腑銆傚叾瀹冩敮鎸亀ord鏍煎紡鐨勭函java鏂規(guī)榪樻湁itext錛屼笉榪囦篃鏄粛鍦ㄥ鏂椾腑銆備絾鏄疕SSF宸茬粡鎴愮啛鍒拌兘澶熷拰瓚沖鎴戜滑浣跨敤浜嗐傚彟澶栵紝鏃犻敗姘鎬腑Office鐨勫疄鐜版柟妗堜篃鏄函java鐨勮В鍐蟲柟妗堬紝涓嶈繃閭d篃鏄畬鍏ㄥ晢涓氱殑浜у搧錛屽茍涓嶆槸鍏紑浠g爜欏圭洰銆傚叾瀹烇紝浠庡紑鍙戝巻鍙茬殑瑙掑害璁詫紝鍦?0騫翠唬涓湡starOffice鐨勫師浣滆呭湪寰峰浗鎴愮珛浜哠tarOffice suite鍏徃錛岀劧鍚庡埌1999騫村澶﹕tarOffice琚玸un鏀惰喘錛屽啀鍒?000騫?鏈坰tarOffice5.2鐨勫彂甯冿紱騫朵笖浠巗tarOffice6.0寮濮嬶紝starOffice寤虹珛鍦∣penOffice鐨刟pi鐨勫熀紜涓婏紝榪欎釜鍏紑浠g爜鐨刼ffice欏圭洰宸茬粡榪涜浜嗗緢闀跨殑鏃墮棿銆傝櫧鐒墮偅鏄敱C++鍐欑殑錛屼絾鏄疨OI鐨勪唬鐮侀儴鍒嗕篃鏄敱openOffice鏀硅繃鏉ョ殑銆傛墍浠ワ紝搴旇瀵筆OI鍏呮弧瓚沖鐨勪俊蹇冦傚浗鍐呭凡緇忔湁閮ㄥ垎鍏徃鍦ㄤ粬浠殑鍔炲叕鑷姩鍖栫瓑Web欏圭洰涓嬌鐢╬oi浜嗭紝濡傛棩鎭掔殑ioffice錛屾搗娉扮殑HTOffice絳夈?br />
    java褰撳垵鎶婃牳蹇冨鐞嗚鎴怳nicode錛屽甫鏉ョ殑濂藉鏄彟浠g爜閫傚簲浜嗗璇█鐜銆傜劧鑰岀敱浜庤佸鐨勮嫳璇彧鏈?6涓瓧姣嶏紝鏈変簺鎯呭喌涓嬶紝涓浜涚▼搴忓憳鐢?浣嶇殑byte澶勭悊錛屼竴涓嶅皬蹇冨氨鍘繪帀浜咰JK鐨勯珮浣嶃傛垨鑰呮槸鐢變簬涔?fàn)鎯湪绋嬪簭涓噰鐢嫭缂栫爜锛寴q樻湁澶氱鍘熷洜錛屼嬌寰楄澶歫ava搴旂敤鍦–JK鐨勫鐞嗕笂寰堢儲鎭箋傝繕濂藉湪POI HSSF涓冭檻鍒拌繖涓棶棰橈紝鍙互璁劇疆encoding涓哄弻瀛楄妭銆?br />
    POI鍙互鍒皐ww.apache.org涓嬭澆鍒般傜紪璇戝ソ鐨刯ar涓昏鏈夎繖鏍?涓細poi鍖?poi Browser鍖?poi hdf鍖?poi hssf渚嬬▼鍖呫傚疄闄呰繍琛屾椂錛岄渶瑕佹湁poi鍖呭氨鍙互浜嗐傚鏋滅敤Jakarta ant緙栬瘧鍜岃繍琛岋紝涓嬭澆apache Jakarta POI鐨剅elease涓殑src鍖咃紝瀹冮噷闈㈠凡緇忎負浣犵敓鎴愬ソ浜哹uild鏂囦歡浜嗐傚彧瑕佽繍琛宎nt灝卞彲浠ヤ簡(ant 鐨勫畨瑁呭拰浣跨敤鍦ㄦ涓嶈浜?銆傚鏋滄槸鐢↗builder 榪愯錛岃鍦ㄦ柊寤虹殑欏圭洰涓姞鍏oi鍖呫備互Jbuilder6涓轟緥錛岄夋嫨Tools鑿滃崟欏圭殑config libraries...閫夐」錛屾柊寤轟竴涓猯ib銆傚湪寮瑰嚭鐨勮彍鍗曚腑閫夋嫨poi鍖咃紝濡傝繖涓猨akarta-poi-1.5.1-final-20020820.jar錛屾妸poi娣誨姞鍒癹builder涓傜劧鍚庯紝鍙抽敭鐐瑰嚮浣犵殑欏圭洰錛屽湪project鐨刾roperties鑿滃崟涓璸ath鐨剅equired Libraries涓紝鐐筧dd錛屾坊鍔犲垰鎵嶅姞鍏ュ埌jbuilder涓殑poi鍒頒綘鐜板湪鐨勯」鐩腑銆傚鏋滀綘浠呬粎鏄負浜嗙啛鎮(zhèn)塒OI hssf鐨勪嬌鐢紝鍙互鐩存帴鐪婸OI鐨剆amples鍖呬腑鐨勬簮浠g爜錛屽茍涓旇繍琛屽畠銆俬ssf鐨勫悇縐嶅璞¢兘鏈変緥紼嬬殑浠嬬粛銆俬ssf鎻愪緵鐨勪緥紼嬪湪org.apache.poi.hssf.usermodel.examples鍖呬腑錛屽叡鏈?4涓紝鐢熸垚鐨勭洰鏍噚ls閮芥槸workbook.xls銆傚鏋滀綘鎯崇湅鏇村鐨勪緥紼嬶紝鍙互鍙傝僪ssf鐨凧unit test cases錛屽湪poi鐨勫寘鐨勬簮浠g爜涓湁銆俬ssf閮芥湁嫻嬭瘯浠g爜銆?br />
    榪欓噷鍙閮ㄥ垎渚嬬▼鐨勫疄鐜板仛浠嬬粛銆?br />
    HSSF鎻愪緵緇欑敤鎴蜂嬌鐢ㄧ殑瀵硅薄鍦╫rg.apache.poi.hssf.usermodel鍖呬腑,涓昏閮ㄥ垎鍖呮嫭Excell瀵硅薄錛屾牱寮忓拰鏍煎紡錛岃繕鏈夎緟鍔╂搷浣溿傛湁浠ヤ笅鍑犵瀵硅薄錛?br />
    HSSFWorkbook excell鐨勬枃妗e璞?br />
    HSSFSheet excell鐨勮〃鍗?br />
    HSSFRow excell鐨勮

    HSSFCell excell鐨勬牸瀛愬崟鍏?br />
    HSSFFont excell瀛椾綋

    HSSFName 鍚嶇О

    HSSFDataFormat 鏃ユ湡鏍煎紡

    鍦╬oi1.7涓墠鏈変互涓?欏癸細

    HSSFHeader sheet澶?br />
    HSSFFooter sheet灝?br />
    鍜岃繖涓牱寮?br />
    HSSFCellStyle cell鏍峰紡

    杈呭姪鎿嶄綔鍖呮嫭

    HSSFDateUtil 鏃ユ湡

    HSSFPrintSetup 鎵撳嵃

    HSSFErrorConstants 閿欒淇℃伅琛?br />
    浠旂粏鐪媜rg.apache.poi.hssf鍖呯殑緇撴瀯錛屼笉闅懼彂鐜癏SSF鐨勫唴閮ㄥ疄鐜伴伒寰殑鏄疢VC妯″瀷銆?br />
    榪欓噷鎴戠敤Rose鎶妎rg.apache.poi.hssf.usermodel鍖呬腑鐨勫璞″弽鍚戝鍏ュ茍鏍規(guī)嵁鐩鎬簰鍏崇郴浣滀簡鏁寸悊錛岃瑙佷笅闈袱鍥撅細

    聽聽IMG ../upload/article/a2005512101549.jpg[/IMG]

    鍥? 鍩烘湰瀵硅薄

    浠庝腑涓嶉毦鍙互鍙戠幇姣忎竴涓熀鏈璞¢兘鍏寵仈浜嗕竴涓猂ecord瀵硅薄銆俁ecord瀵硅薄鏄竴涓弬鑰僌ffice鏍煎紡鐨勭浉鍏寵褰曘?br />
    聽聽 IMG ../upload/article/2005512101631.jpg[/IMG]


    鍥? HSSFWorkbook

    HSSFWorkbook鍗蟲槸涓涓狤xcell瀵硅薄銆傝繖騫呯被鍥句綋鐜扮殑鏄疕SSFWorkbook鍜屽熀鏈璞$殑鐩鎬簰鍏崇郴銆傚彲瑙侊紝璁稿瀵硅薄涓篃寤虹珛浜哤orkbook鐨勫紩鐢ㄣ傝繕闇瑕佹敞鎰忕殑鏄湪HSSFWorkbook鍜孒SSFSheet涓緩绔嬩簡log鏈哄埗POILogger錛岃屼笖POILogger涔熸槸浣跨敤apache Log4J瀹炵幇鐨勩?br />
    鍏堢湅poi鐨別xamples鍖呬腑鎻愪緵鐨勬渶綆鍗曠殑渚嬪瓙錛屽緩绔嬩竴涓┖xls鏂囦歡銆?br />
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    import java.io.FileOutputStream;

    import java.io.IOException;

    public class NewWorkbook

    {

    public static void main(String[] args)

    throws IOException

    {

    HSSFWorkbook wb = new HSSFWorkbook();//寤虹珛鏂癏SSFWorkbook瀵硅薄

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");

    wb.write(fileOut);//鎶奧orkbook瀵硅薄杈撳嚭鍒版枃浠秝orkbook.xls涓?br />
    fileOut.close();

    }

    }

    閫氳繃榪欎釜渚嬪瓙錛屾垜浠緩绔嬬殑鏄竴涓┖鐧界殑xls鏂囦歡錛堜笉鏄┖鏂囦歡錛夈傚湪姝ゅ熀紜涓婏紝鎴戜滑鍙互榪涗竴姝ョ湅鍏跺畠鐨勪緥瀛愩?br />

    import org.apache.poi.hssf.usermodel.*;

    import java.io.FileOutputStream;

    import java.io.IOException;

    public class CreateCells

    {

    public static void main(String[] args)

    throws IOException

    {

    HSSFWorkbook wb = new HSSFWorkbook();//寤虹珛鏂癏SSFWorkbook瀵硅薄

    HSSFSheet sheet = wb.createSheet("new sheet");//寤虹珛鏂扮殑sheet瀵硅薄


    // Create a row and put some cells in it. Rows are 0 based.

    HSSFRow row = sheet.createRow((short)0);//寤虹珛鏂拌

    // Create a cell and put a value in it.

    HSSFCell cell = row.createCell((short)0);//寤虹珛鏂癱ell

    cell.setCellValue(1);//璁劇疆cell鐨勬暣鏁扮被鍨嬬殑鍊?br />

    // Or do it on one line.

    row.createCell((short)1).setCellValue(1.2);//璁劇疆cell嫻偣綾誨瀷鐨勫?br />
    row.createCell((short)2).setCellValue("test");//璁劇疆cell瀛楃綾誨瀷鐨勫?br />
    row.createCell((short)3).setCellValue(true);//璁劇疆cell甯冨皵綾誨瀷鐨勫?

    HSSFCellStyle cellStyle = wb.createCellStyle();//寤虹珛鏂扮殑cell鏍峰紡

    cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//璁劇疆cell鏍峰紡涓哄畾鍒剁殑鏃ユ湡鏍煎紡

    HSSFCell dCell =row.createCell((short)4);

    dCell.setCellValue(new Date());//璁劇疆cell涓烘棩鏈熺被鍨嬬殑鍊?br />
    dCell.setCellStyle(cellStyle); //璁劇疆璇ell鏃ユ湡鐨勬樉紺烘牸寮?br />
    HSSFCell csCell =row.createCell((short)5);

    csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//璁劇疆cell緙栫爜瑙e喅涓枃楂樹綅瀛楄妭鎴柇

    csCell.setCellValue("涓枃嫻嬭瘯_Chinese Words Test");//璁劇疆涓タ鏂囩粨鍚堝瓧絎︿覆

    row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//寤虹珛閿欒cell


    // Write the output to a file

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");

    wb.write(fileOut);

    fileOut.close();

    }

    }

    鎴戠◢寰慨鏀逛簡鍘熸潵鐨別xamples鍖呬腑鐨凜reateCells綾誨啓浜嗕笂闈㈢殑鍔熻兘嫻嬭瘯綾匯傞氳繃榪欎釜渚嬪瓙錛屾垜浠彲浠ユ竻妤氱殑鐪嬪埌xls鏂囦歡浠庡ぇ鍒板皬鍖呮嫭浜咹SSFWorkbook HSSFSheet HSSFRow HSSFCell榪欐牱鍑犱釜瀵硅薄銆傛垜浠彲浠ュ湪cell涓緗悇縐嶇被鍨嬬殑鍊箋傚挨鍏惰娉ㄦ剰鐨勬槸濡傛灉浣犳兂姝g‘鐨勬樉紺洪潪嬈х編鐨勫瓧絎︽椂錛屽挨鍏惰薄涓棩闊╄繖鏍風(fēng)殑璇█錛屽繀欏昏緗紪鐮佷負16浣嶇殑鍗蟲槸HSSFCell.ENCODING_UTF_16錛屾墠鑳戒繚璇佸瓧絎︾殑楂?浣嶄笉琚埅鏂屽紩璧風(fēng)紪鐮佸け鐪熷艦鎴愪貢鐮併?br />
    鍏朵粬嫻嬭瘯鍙互閫氳繃鍙傝僥xamples鍖呬腑鐨勬祴璇曚緥瀛愭帉鎻oi鐨勮緇嗙敤娉曪紝鍖呮嫭瀛椾綋鐨勮緗紝cell澶у皬鍜屼綆綰圭殑璁劇疆絳夈傞渶瑕佹敞鎰忕殑鏄疨OI鏄竴涓粛鐒跺湪瀹屽杽涓殑鍏紑浠g爜鐨勯」鐩紝鎵浠ユ湁浜涘姛鑳芥鍦ㄤ笉鏂殑鎵╁厖銆傚HSSFSheet鐨刧etFooter() getHeader()鍜宻etFooter(HSSFFooter hsf) setHeader(HSSFHeader hsh)鏄湪POI1.7涓墠鏈夌殑錛岃孭OI1.5涓氨娌℃湁銆傝繍琛屾祴璇曠啛鎮(zhèn)変唬鐮佹垨鑰呬嬌鐢ㄥ畠鍋氶」鐩椂璇鋒敞鎰廝OI鐨勭増鏈?br />
    鍙﹀闇瑕佹敞鎰忕殑鏄疕SSF涔熸湁瀹冪殑瀵箈ls鍩轟簬浜嬩歡鐨勮В鏋愩傚彲浠ュ弬鑰冧緥紼嬩腑鐨凟ventExample.java銆傚畠閫氳繃瀹炵幇HSSFListener瀹屾垚浠庢櫘閫氭祦璁ょ煡Xls涓寘鍚殑鍐呭錛屽湪apache Cocoon涓殑org.apache.cocoon.serialization.HSSFSerializer涓敤鍒頒簡榪欎釜瑙f瀽銆傚洜涓篊ocoon2鏄熀浜庝簨浠剁殑錛屾墍浠OI涓轟簡鎻愪緵蹇熺殑瑙f瀽涔熸彁渚涗簡鐩稿簲鐨勪簨浠躲傚綋鐒舵垜浠嚜宸變篃鍙互瀹炵幇榪欎釜浜嬩歡鎺ュ彛銆?br />
    鍥犱負POI榪樹笉鏄竴涓凍澶熸垚鐔熺殑欏圭洰錛屾墍浠ユ湁蹇呰鍋氳繘涓姝ョ殑寮鍙戝拰嫻嬭瘯銆備絾鏄畠宸茬粡涓烘垜浠敤綰痡ava鎿嶄綔ole2瀵硅薄鎻愪緵浜嗗彲鑳斤紝鑰屼笖鍏嬫湇浜唎le瀵硅薄璋冪敤鐨勭己闄鳳紝鎻愪緵浜嗘湇鍔″櫒绔殑Excel瑙e喅鏂規(guī)銆?br />



    brock 2006-07-26 15:29 鍙戣〃璇勮
    ]]>
    Excel鐨刯ava澶勭悊鏂瑰紡http://www.tkk7.com/brock/archive/2006/07/13/57961.htmlbrockbrockThu, 13 Jul 2006 05:48:00 GMThttp://www.tkk7.com/brock/archive/2006/07/13/57961.htmlhttp://www.tkk7.com/brock/comments/57961.htmlhttp://www.tkk7.com/brock/archive/2006/07/13/57961.html#Feedback0http://www.tkk7.com/brock/comments/commentRss/57961.htmlhttp://www.tkk7.com/brock/services/trackbacks/57961.html Poi鍗硃oor object interface涔嬫剰錛屾槸poi欏圭洰緇勫寰蔣灝侀棴鐨刼ffice鏂囦歡鏍煎紡錛堟帴鍙o級鐨勭О璋擄紒
    鍏朵腑鎻愪緵浜嗗word鍜宔xcel鐨刯ava鎺ュ彛錛岀敤娉曢潪甯哥畝鍗曪紝騫朵笖鏄畬鍏ㄥ厤璐圭殑錛屽涓枃鐨勬敮鎸佷篃鐩稿綋涓嶉敊錛屼笅闈㈡槸澶勭悊excel涓涓畝鍗曠殑瀹炰緥錛?


    package test;

    import java.io.*;
    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;

    public class Test {
    public static void main(String[] args) {
    try {
    /**************鍒涘緩涓涓獂ls鏂囨。*************/
    HSSFWorkbook wb = new HSSFWorkbook();
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow((short)0);
    HSSFCell cell = row.createCell((short)0);
    row.createCell((short)1);
    row.createCell((short)2);
    row.createCell((short)3);
    row.createCell((short)4);
    row.createCell((short)5);
    cell.setCellValue("test_write!");
    wb.write(fileOut);
    fileOut.close();
    /*************璇誨彇騫朵慨鏀箈ls鏂囨。***************/
    POIFSFileSystem pfs=new POIFSFileSystem(new FileInputStream("workbook.xls"));
    HSSFWorkbook hwb=new HSSFWorkbook(pfs);
    HSSFSheet hs=hwb.getSheetAt(0);
    HSSFRow hr=hs.getRow(0);
    HSSFCellStyle style=hwb.createCellStyle();
    style.setAlignment(style.ALIGN_CENTER);
    HSSFFont hf=hwb.createFont();
    hf.setFontName("妤蜂綋_GB2312");
    style.setFont(hf);
    HSSFCell cl=hr.getCell((short)2);
    cl.setEncoding(cl.ENCODING_UTF_16);
    cl.setCellStyle(style);
    cl.setCellValue("test_modify! 嫻嬭瘯\u2014\u2014緙栬緫錛?);
    FileOutputStream fos=new FileOutputStream("workbook.xls");
    hwb.write(fos);
    fos.flush();
    fos.close();
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    }



    brock 2006-07-13 13:48 鍙戣〃璇勮
    ]]>
    主站蜘蛛池模板: 亚洲国产成人久久精品app| 91精品免费观看| 亚洲天堂2016| 亚洲成A∨人片在线观看不卡| 日韩视频免费在线| 四虎精品视频在线永久免费观看| 国产日韩久久免费影院| 亚洲AV无码一区二区三区牲色 | 亚洲国产精品lv| 亚洲精品无码久久久| 卡一卡二卡三在线入口免费| 91精品导航在线网址免费| 国产黄片不卡免费| 看成年女人免费午夜视频| 最新亚洲精品国偷自产在线| 亚洲狠狠ady亚洲精品大秀| 亚洲男人的天堂www| 亚洲男人的天堂一区二区| 国产精品免费视频一区| 国内自产拍自a免费毛片| 99久久免费国产香蕉麻豆| 99视频有精品视频免费观看| 最近免费中文字幕中文高清| 美女免费视频一区二区| 亚洲爆乳无码专区www| 在线亚洲午夜片AV大片| 亚洲第一成人在线| 亚洲1234区乱码| 亚洲av无码专区在线| 亚洲一区电影在线观看| 亚洲依依成人精品| 亚洲一卡2卡4卡5卡6卡残暴在线| 亚洲综合综合在线| 亚洲成a人片在线观| 亚洲性69影院在线观看| 亚洲乱码中文论理电影| 在线亚洲午夜片AV大片| 亚洲成aⅴ人片久青草影院按摩| 亚洲欧美乱色情图片| 国产成人精品日本亚洲语音 | 夭天干天天做天天免费看|