码迷,mamicode.com
首页 > 编程语言 > 详细

java操作excel2010

时间:2014-05-21 11:34:02      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   c   code   java   

首先需要下载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

标签:style   blog   class   c   code   java   

原文地址:http://blog.csdn.net/ijustyce/article/details/26365603

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!