poi3.7处理excel文件

poi是apache下的一个开源处理excel的java框架

http://poi.apache.org/

下面是一个示例代码.

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class TestExcel {

	public static void main(String[] args) throws Exception{
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet();
		workbook.setSheetName(0, "我的工作簿");
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell;

		//写入2列的第一行
		cell = row.createCell(0);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("第一列");
		
		cell = row.createCell(1);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("第二列");	
			
		//增加 第二行
		row = sheet.createRow(1);
		cell = row.createCell(0);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("1");
		cell = row.createCell(1);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("2");


		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		workbook.write(baos);
		byte[] b = baos.toByteArray();
		File file= new File("c:/myExcel.xlsg");
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(b);
	}

}

相关推荐