欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poi之Excel操作

程序员文章站 2022-07-13 11:52:12
...
package com.test;

import org.apache.poi2.hssf.usermodel.HSSFCell;
import org.apache.poi2.hssf.usermodel.HSSFRow;
import org.apache.poi2.hssf.usermodel.HSSFSheet;
import org.apache.poi2.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;

public class CreateExcel {

	public static void main(String[] args) {
		try {
			HSSFWorkbook book = new HSSFWorkbook();
			HSSFSheet sheet = book.createSheet();
			HSSFRow row;
			HSSFCell cell;
			for (int i = 0 ; i < 10 ; i++) {
				row = sheet.createRow((short)i);
				for (int j = 0 ; j < 10 ; j++) {
					cell = row.createCell((short)j);
					cell.setCellValue("cell_"+j);
				}
			}
			FileOutputStream fos = new FileOutputStream("c:/demo.xls");
			book.write(fos);
			fos.close();
			System.out.println("OK!");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}