javaexcelapi

package test;

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, "gradeList");
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell;

		//写入2列的第一行
		for (int i = 0; i < 8; i++) {
			cell = row.createCell(i);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue("第"+(i+1)+"列");
		}
		
		//增加 第二行
		for (int j = 1; j < 20; j++) {
			row = sheet.createRow(j);
			
			for (int i = 0; i < 8; i++) {
				cell = row.createCell(i);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(i+1);
		}
		
		}
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		workbook.write(baos);
		byte[] b = baos.toByteArray();
		File file= new File("c:/myExcel.xls");
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(b);
	}

}

你可能感兴趣的:(java,apache,C++,c,J#)