官方demo 创建一个excel


//创建一个文件
try {
FileOutputStream out = new FileOutputStream("workbook1.xls");
//创建一个//创建一页
Workbook wb = new HSSFWorkbook();
//创建一页
Sheet s = wb.createSheet();
//声明一个引用对象row
Row r = null;
//声明一个引用对象cell
Cell c = null;
//创建3个cell样式对象
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
CellStyle cs3 = wb.createCellStyle();
DataFormat df = wb.createDataFormat();
//创建2个字体样式
Font f = wb.createFont();
Font f2 = wb.createFont();
//设置字体f的具体大小样式
f.setFontHeightInPoints((short) 12);
f.setColor( (short)0xc );
// make it bold
//arial is the default font 设置字体粗细
f.setBoldweight(Font.BOLDWEIGHT_BOLD);

//set font 2 to 10 point type 设置字体大小
f2.setFontHeightInPoints((short) 10);
//make it red 设置字体颜色
f2.setColor( (short)Font.COLOR_RED );
//make it bold 设置字体粗细
f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
//在字上画横线
f2.setStrikeout( true );

//set cell stlye 设置单元格样式
cs.setFont(f);
//set the cell format 格式化单元格数据
cs.setDataFormat(df.getFormat("#,##0.0"));

//set a thin border
cs2.setBorderBottom(cs2.BORDER_THIN);
//fill w fg fill color 设置单元格背景颜色
cs2.setFillPattern((short) CellStyle.THICK_BACKWARD_DIAG);
//set the cell format to text see DataFormat for a full list 设置单元格内容格式这里为文本格式
cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));

// set the font 为单元格样式cs2添加字体样式f2
cs2.setFont(f2);

// set the sheet name in Unicode 设置sheet名称
wb.setSheetName(0, "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F " +
"\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430" );
// in case of plain ascii
// wb.setSheetName(0, "HSSF Test");
// create a sheet with 30 rows (0-29) 创建一个sheet为30行
int rownum;
for (rownum = (short) 0; rownum < 30; rownum++){
// create a row 创建行
r = s.createRow(rownum);
// on every other row
if ((rownum % 2) == 0)
{
// make the row height bigger (in twips - 1/20 of a point)
//设置行高
r.setHeight((short) 0x249);
}

// r.setRowNum(( short ) rownum);
// create 10 cells (0-9) (the += 2 becomes apparent later
for (short cellnum = (short) 0; cellnum < 10; cellnum += 2)
{
// create a numeric cell 创建几个单元格
c = r.createCell(cellnum);
// do some goofy math to demonstrate decimals 为单元格设置值
c.setCellValue(rownum * 10000 + cellnum
+ (((double) rownum / 1000)
+ ((double) cellnum / 10000)));

String cellValue;

// create a string cell (see why += 2 in the
c = r.createCell((short) (cellnum + 1));

// on every other row
if ((rownum % 2) == 0)
{
// set this cell to the first cell style we defined
c.setCellStyle(cs);
// set the cell's string value to "Test"
c.setCellValue( "Test" );
}
else
{
c.setCellStyle(cs2);
// set the cell's string value to "\u0422\u0435\u0441\u0442"
c.setCellValue( "\u0422\u0435\u0441\u0442" );
}


// make this column a bit wider
s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
}
}

//draw a thick black border on the row at the bottom using BLANKS
// advance 2 rows
rownum++;
rownum++;

r = s.createRow(rownum);

// define the third style to be the default
// except with a thick black border at the bottom
cs3.setBorderBottom(cs3.BORDER_THICK);

//create 50 cells
for (short cellnum = (short) 0; cellnum < 50; cellnum++)
{
//create a blank type cell (no value)
c = r.createCell(cellnum);
// set it to the thick black border style
c.setCellStyle(cs3);
}

//end draw thick black border


// demonstrate adding/naming and deleting a sheet
// create a sheet, set its title then delete it
s = wb.createSheet();
wb.setSheetName(1, "DeletedSheet");
wb.removeSheetAt(1);
//end deleted sheet

// write the workbook to the output stream
// close our file (don't blow out our file handles
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值