public static void createExcel(OutputStream os) throws IOException,
RowsExceededException, WriteException
{
// 创建工作区
WritableWorkbook workbook = Workbook.createWorkbook(os);
// 创建新的一页,sheet只能在工作簿中使用
WritableSheet sheet = workbook.createSheet("test sheet1", 0);
// 通过函数WritableFont()设置字体样式
// 第一个参数表示所选字体
// 第二个参数表示字体大小
// 第三个参数表示粗体样式,有BOLD和NORMAL两种样式
// 第四个参数表示是否斜体,此处true表示为斜体
// 第五个参数表示下划线样式
// 第六个参数表示颜色样式,此处为Red
WritableFont wf = new WritableFont(WritableFont.TIMES, 18,
WritableFont.BOLD, true, UnderlineStyle.NO_UNDERLINE,
Colour.RED);
CellFormat cf = new WritableCellFormat(wf);
// 创建单元格即具体要显示的内容,new Label(0,0,"用户") 第一个参数是column 第二个参数是row
// 第三个参数是content,第四个参数是可选项,为Label添加字体样式
WritableCell employee = new Label(0, 0, "雇员", cf);
// 通过sheet的addCell方法添加Label,注意一个cell/label只能使用一次addCell
sheet.addCell(employee);
WritableCell sex = new Label(1, 0, "性别");
sheet.addCell(sex);
// 将内容写到输出流中,然后关闭工作区,最后关闭输出流
<span style="color: #ff0000;">workbook.write();
workbook.close();</span>
os.close();
}
生成简单的Excel文件的示例
最新推荐文章于 2023-04-24 18:24:31 发布