-->

本文实现了使用java 从数据库中获得对象,并存入集合中,

然后输出到Excel,并设置样式

package com.webwork; import java.io.File;
import java.io.IOException;
import java.util.List; import org.jdom.output.Format; import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException; public class Jxls {
public static void main(String[] args) {
try {
new Jxls().writeExcel();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeExcel() throws RowsExceededException, WriteException, IOException{
new Jdbc().addList();
List< Student> stuLists = Jdbc.getStuList();
WritableWorkbook book =null; book = Workbook.createWorkbook(new File("e:/io/stuInfo.xls")); WritableSheet sheet = book.createSheet("studentInfo", 0); /*
* format设置样式
*/
//设置字体
WritableFont font1 = new WritableFont(WritableFont.ARIAL,18,WritableFont.BOLD);
WritableFont font2 = new WritableFont(WritableFont.ARIAL, 13, WritableFont.BOLD);
//格式化单元格
WritableCellFormat format1 = new WritableCellFormat(font1);
WritableCellFormat format2 = new WritableCellFormat(font2);
WritableCellFormat format3 = new WritableCellFormat();
//设置样式
format1.setAlignment(jxl.format.Alignment.CENTRE);//水平对齐方式
format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直对齐方式
format1.setBackground(jxl.format.Colour.LIGHT_BLUE);//设置背景颜色
format1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);//设置边框 //合并单元格第一行
sheet.mergeCells(0, 0, 3, 0);
Label label = new Label(0, 0, "yc95全班信息表",format1);
sheet.addCell(label); //设置每列小标题
String [] str = {"id","name","sex","age"};
for (int i = 0; i < str.length; i++) {
sheet.addCell(new Label(i, 1, str[i]));
sheet.setColumnView(i, 20);//设置列宽
} for (int i = 2; i < stuLists.size(); i++) {
Student s = stuLists.get(i);
//System.out.println(s);
Label label1 = new Label(0,i ,s.getId());
Label label2 = new Label(1,i, s.getName());
Label label3 = new Label(2,i, s.getSex());
Label label4 = new Label(3,i, s.getAge());
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
}
book.write();
book.close();
System.out.println("插入excel数据成功!!!");
}
}

下面是excel中得到的内容

输出到Excel后,相应的就是从Excel中去取数据

package com.webwork; import java.io.File;
import java.io.IOException;
import java.util.List; import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException; public class ReadExcel {
public static void main(String[] args) {
List stuLists = Jdbc.getStuList();
Workbook book = null;
try {
book = Workbook.getWorkbook(new File("e:/io/stuInfo.xls"));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet[] sheets = book.getSheets();
for (int i = 0; i < sheets.length; i++) {
Sheet sheet = sheets[i];
int rows= sheet.getRows();
for (int j = 1; j < rows; j++) {
Cell[] cells= sheet.getRow(j);
for (int k = 0; k < cells.length; k++) {
Cell cell = cells[k];
String id = cell.getContents();
System.out.print(id+"\t");
} System.out.println();
} } }
}

输出的结果如下:

-->