|
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
* @author 刘毅
* @date 2010-1-12
* @param
*/
public class CreateExcelForCreateCells {
/***
* 创建空的xls格式的文档,并根据行号创建cell并且赋值
*/
public void createXlsForCell(){
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
//创建行,并在第几行创建
Row row = sheet.createRow(1);
//创建行cells,初始值为0.
Cell cell = row.createCell(0);
//开始赋值
cell.setCellValue(1);
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(createHelper.createRichTextString("1111111"));
row.createCell(3).setCellValue(111);
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream("src/workbook.xls");
wb.write(fileOut);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(null != fileOut){
fileOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
CreateExcelForCreateCells create = new CreateExcelForCreateCells();
create.createXlsForCell();
}
} |
|