JXL导出表格
JXL导出表格,测试信息写屎了,记得导入jxl.jar包
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class ExportData {
/**
* @param args
*/
public static void exportExcel(String filename) {
String[] listThead = { "第一列", "第二列", "第三列", "第四列" };
List<int[]> queryAllList = new ArrayList<int[]>();
jxl.write.Label label;
WritableWorkbook wwb;
FileOutputStream fos;
try {
fos = new FileOutputStream(filename);
wwb = Workbook.createWorkbook(fos);
WritableSheet sheet = wwb.createSheet("测试表", 10);// 创建一个工作表
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.createFont("宋体"),
12, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLUE);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setBackground(Colour.GRAY_25);
// 设置边框
wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
// 居中
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setAlignment(Alignment.CENTRE);
// 设置默认行列的高度和宽度
sheet.getSettings().setDefaultColumnWidth(30);
// 添加表头
if (listThead != null) {
for (int i = 0; i < listThead.length; i++) {
label = new Label(i, 0, listThead[i]);
try {
sheet.addCell(label);
} catch (Exception e) {
e.printStackTrace();
}
}
}
queryAllList.add(new int[] { 1, 1, 1, 1 });
queryAllList.add(new int[] { 2, 2, 2, 2 });
queryAllList.add(new int[] { 1, 4, 3, 1 });
queryAllList.add(new int[] { 1, 6, 5, 4 });
// 构造表体
if (queryAllList != null) {
for (int j = 0; j < queryAllList.size(); j++) {
for (int k = 0; k < (queryAllList.get(j)).length; k++) {
label = new Label(k,j + 1,(queryAllList.get(j))[k]+"");
try {
sheet.addCell(label);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
try {
wwb.write();
wwb.close();
} catch (IOException ioe) {
System.out.println("ioe出现异常");
ioe.printStackTrace();
} catch (WriteException we) {
System.out.println("we出现异常");
we.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 从Excel文件里读取数据保存到Vector里
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
exportExcel("E:\\TEST.xls");
System.out.println("成功");
}
}