java操作excel2010

首先需要下载poi,我建议使用我使用的,以免出现其他问题!以下代码测试正常!

package com.ijustyce.xlsx;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

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.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
public class xlsx {

	public static void main(String[] args) {
		
		File file = new File("d:\\tmp.xlsx");
		String[][] value = {{"AA" , "AB"},{"BA" , "BB"},{"CA"},{"DA","DB","DC"}};
		writeXlsx(value, file , false);
	}
	
	/**
	 * 
	 * @param value the value you want to write to xlsx file ! make sure 
	 * @param xlsxFile
	 * @param overwrite
	 * @return
	 */
	public static boolean writeXlsx(String[][]value , File xlsxFile ,boolean overwrite) {
		int rowNum = value.length;
		if (xlsxFile.exists()&&!overwrite) {
			System.out.println("xlsx file already exists , please delete it or use a new name , "
					+ "if you want to overwrite it , please change overwrite to true!");
			return false;
		}
		SXSSFWorkbook wb = new SXSSFWorkbook();
		Sheet sh = wb.createSheet();
        for(int rowCount = 0; rowCount < rowNum ; rowCount++){
            Row row = sh.createRow(rowCount);
            int column = value[rowCount].length;
            for(int columnCount = 0; columnCount < column; columnCount++){
                Cell cell = row.createCell(columnCount);
                cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(value[rowCount][columnCount]);
            }
        }
        try {
			FileOutputStream fOut = new FileOutputStream(xlsxFile.getAbsolutePath());
			wb.write(fOut);  
            fOut.flush();  
            fOut.close(); 
            System.out.println("write success no error occur!");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

}

需要导入这些包:


1、dom4j-1.6.1.jar

2、poi-3.10-FINAL-20140208.jar

3、poi-examples-3.10-FINAL-20140208.jar

4、poi-excelant-3.10-FINAL-20140208.jar

5、poi-ooxml-3.10-FINAL-20140208.jar

6、poi-ooxml-schemas-3.10-FINAL-20140208.jar

7、poi-scratchpad-3.10-FINAL-20140208.jar

8、xmlbeans-2.3.0.jar

这些包的下载地址:http://download.csdn.net/detail/justyce/7374345

java操作excel2010,布布扣,bubuko.com

java操作excel2010

上一篇:javascript——从「最被误解的语言」到「最流行的语言」


下一篇:springMVC注解优化