java生成excel模板
依赖
<!--jxl 生成模板文件-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
代码
package com.liujinghe.excel;
import jxl.*;
import jxl.write.*;
import java.io.*;
public class ExcelHandle {
public ExcelHandle() {
}
/**
* 输出Excel
*/
public static void writeExcel(OutputStream os) {
try {
/** 只能通过API提供的 工厂方法来创建Workbook,而不能使用WritableWorkbook的构造函数,因为类WritableWorkbook的构造函数为 protected类型:方法一:直接从目标文件中读取 WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile));方法 二:如下实例所示 将WritableWorkbook直接写入到输出流*/
WritableWorkbook wwb = Workbook.createWorkbook(os);
//创建Excel工作表 指定名称和位置
WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
/**************往工作表中添加数据*****************/
//1.添加Label对象
//Twitter完整URL Facebook完整URL Facebook完整URL
Label label = new Label(0, 0, "账号类型");
Label label1 = new Label(1, 0, "姓名");
Label label2 = new Label(2, 0, "年龄");
Label label3 = new Label(3, 0, "性别");
ws.addCell(label);
ws.addCell(label1);
ws.addCell(label2);
ws.addCell(label3);
//7.写入工作表
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//测试
public static void main(String args[]) {
try {
//输出EXCEL
File filewrite = new File("D:/账号模板.xls");
filewrite.createNewFile();
OutputStream os = new FileOutputStream(filewrite);
ExcelHandle.writeExcel(os);
} catch (Exception e) {
e.printStackTrace();
}
}
}
浙公网安备 33010602011771号