求poi操作excel的源代码

解决方案 »

  1.   

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;import junit.framework.Assert;import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.util.CellReference;
    import org.apache.poi.xssf.streaming.SXSSFWorkbook;public class Excel { public static void main(String[] args) {
    Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory,
    Sheet sh = wb.createSheet();
    Sheet sh1 = wb.createSheet();
    for (int rownum = 0; rownum < 1000; rownum++) {
    Row row = sh.createRow(rownum);
    Row row1 = sh1.createRow(rownum);
    for (int cellnum = 0; cellnum < 10; cellnum++) {
    Cell cell = row.createCell(cellnum);
    Cell cell1 = row1.createCell(cellnum);
    String address = new CellReference(cell).formatAsString();
    cell.setCellValue(address);
    String address1 = new CellReference(cell1).formatAsString();
    cell1.setCellValue(address1);
    } } // Rows with rownum < 900 are flushed and not accessible
    for (int rownum = 0; rownum < 900; rownum++) {
    Assert.assertNull(sh.getRow(rownum));
    Assert.assertNull(sh1.getRow(rownum));
    } // ther last 100 rows are still in memory
    for (int rownum = 900; rownum < 1000; rownum++) {
    Assert.assertNotNull(sh.getRow(rownum));
    Assert.assertNotNull(sh1.getRow(rownum));
    } try {
    FileOutputStream out = new FileOutputStream("D:/test1.xlsx");
    wb.write(out);
    out.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }}