最近在写anroid的一个项目里面用到了将数据库中的信息导出到excel表格的操作,就来记录一下java如何控制excel表格
首先你的需要一个外部jar包:jxl-2.6.12.jar
百度网盘下载:链接:https://pan.baidu.com/s/1aD8_Aryz9EhYhAgr6mAaVw
提取码:zrc1
然后将jar包导入到ecplise中,先在项目里面创建一个lib文件夹,接着不用解压直接复制到lib下
然后右键点击构建路径选择添加至构建路径:
然后写代码:
package test;
import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class CreateExcel {
public static void main(String args[]) {
try {
// 打开文件
WritableWorkbook book = Workbook.createWorkbook( new File( " test.xls " ));
WritableSheet sheet = book.createSheet( " 第一页 " , 0 );
Label label1 = new Label( 0 , 0 , " id号" );
sheet.addCell(label1);
Label label2 = new Label( 1 , 0 , " 姓名" );
sheet.addCell(label2);
Label label3 = new Label( 2 , 0 , " 学号" );
sheet.addCell(label3);
Label label4 = new Label( 3 , 0 , " 性别" );
sheet.addCell(label4);
Label label5 = new Label( 4 , 0 , " 电话号" );
sheet.addCell(label5);
Label label6 = new Label( 5 , 0 , " 年" );
sheet.addCell(label6);
Label label7 = new Label( 6 , 0 , " 月" );
sheet.addCell(label7);
Label label8 = new Label( 7 , 0 , " 日" );
sheet.addCell(label8);
//列 行
jxl.write.Number number = new jxl.write.Number( 0, 1 , 12.0 );
jxl.write.Number number1 = new jxl.write.Number( 1, 1 , 12.0 );
jxl.write.Number number2 = new jxl.write.Number( 2, 1 , 12.0 );
jxl.write.Number number3 = new jxl.write.Number( 3, 1 , 12.0 );
jxl.write.Number number4 = new jxl.write.Number( 4, 1 , 12.0 );
jxl.write.Number number5 = new jxl.write.Number( 5, 1 , 12.0 );
jxl.write.Number number6 = new jxl.write.Number( 6, 1 , 12.0 );
jxl.write.Number number7 = new jxl.write.Number( 7, 1 , 12.0 );
sheet.addCell(number);
sheet.addCell(number1);
sheet.addCell(number2);
sheet.addCell(number3);
sheet.addCell(number4);
sheet.addCell(number5);
sheet.addCell(number6);
sheet.addCell(number7);
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
执行后就在这个项目下生成一个test.xls文件,打开后如图:
是不是很容易