import java.io.File;
import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List;
import jxl.Workbook;
import jxl.format.Colour; 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 JXLExportExcel { public static void main(String[] args) throws Exception, WriteException, IOException { Date date = new Date(); long a = date.getTime(); File file = new File(\"c://\" + a + \".xls\"); OutputStream outputStream = new FileOutputStream(file); excel(outputStream); } public static void excel(OutputStream os) throws IOException, RowsExceededException, WriteException { WritableWorkbook workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet(\"学生信息excel\", 0); // 设置字体 WritableFont font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat cellFormat = new WritableCellFormat(font); cellFormat.setBackground(Colour.GREEN); //填充表头的颜色 String title[] = { \"学号\", \"姓名\", \"年龄\", \"学校\",\"address\" }; // 设置表头 Label label = null; for (int i = 0; i < title.length; i++) { label = new Label(i, 0, title[i], cellFormat); sheet.addCell(label); } //从数据库查询出来的数据 List student.setAddress(\"湖南\"+i); list.add(student); } for (int i = 1; i < list.size(); i++) { StudentExcel student = list.get(i); Label label1 = new Label(0, i, String.valueOf(student.getId())); Label label2 = new Label(1, i, student.getName()); Label label3 = new Label(2, i, String.valueOf(student.getAge())); Label label4 = new Label(3, i, student.getSchool()); Label label5 = new Label(4, i, student.getAddress()); sheet.addCell(label1); sheet.addCell(label2); sheet.addCell(label3); sheet.addCell(label4); sheet.addCell(label5); } workbook.write(); workbook.close(); os.close(); } } 因篇幅问题不能全部显示,请点此查看更多更全内容