服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

采用poi创建excel文件

import org.apache.poi.hssf.model.workbook;
import org.apache.poi.hssf.usermodel.*;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.util.date;

public class excelcreate
{
 public static void main(string[] args)throws ioexception
 {
  
  hssfworkbook wb = new hssfworkbook();//建立新hssfworkbook对象
  hssfsheet sheet = wb.createsheet("new sheet");//建立新的sheet对象
  
  
  // create a row and put some cells in it. rows are 0 based.
  hssfrow row = sheet.createrow((short)0);//建立新行
  
  // create a cell and put a value in it.
  hssfcell cell = row.createcell((short)0);//建立新cell
  cell.setcellvalue(1);//设置cell的整数类型的值
  
  
  // or do it on one line.
  row.createcell((short)1).setcellvalue(1.2);//设置cell浮点类型的值
  row.createcell((short)2).setcellvalue("test");//设置cell字符类型的值
  row.createcell((short)3).setcellvalue(true);//设置cell布尔类型的值
  hssfcellstyle cellstyle = wb.createcellstyle();//建立新的cell样式
  workbook workbook = new workbook();
  hssfdataformat hssfdataformat = new hssfdataformat(workbook);
  cellstyle.setdataformat(hssfdataformat.getformat("m/d/yy h:mm"));//设置cell样式为定制的日期格式
  hssfcell dcell =row.createcell((short)4);
  dcell.setcellvalue(new date());//设置cell为日期类型的值
  dcell.setcellstyle(cellstyle); //设置该cell日期的显示格式
  hssfcell cscell =row.createcell((short)5);
  cscell.setencoding(hssfcell.encoding_utf_16);//设置cell编码解决中文高位字节截断
  cscell.setcellvalue("中文测试_chinese words test");//设置中西文结合字符串
  row.createcell((short)6).setcelltype(hssfcell.cell_type_error);//建立错误cell
  
  // write the output to a file
  fileoutputstream fileout = new fileoutputstream("c:/workbook.xls");
  wb.write(fileout);
  fileout.close();
  
 }
}

 

扫描关注微信公众号