import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;
import org.java.team.Writeteam;
import org.java.tianbao.Tianbao;
import org.java.util.DBUtil;
import org.java.xiaowailianjie.WriteXiaowailianjie;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;public classa {public static voidmain(String[] args) throws RowsExceededException, WriteException, IOException {//1. 导出Excel的路径
String filePath = "C:/export.xls";
WritableWorkbook wwb=null;
Connection connection=DBUtil.getConnection();
List excelTests=new ArrayList();
PreparedStatement preparedStatement=null;
String sql="select * from excelTest";
ResultSet resultSet=null;
excelTest exce=null;try{
wwb= Workbook.createWorkbook(newFile(filePath));
}catch(Exception e) {
e.printStackTrace();
}//创建Excel表的"学生"区域的数据
WritableSheet sheet = wwb.createSheet("学生",0);//或者rwb.getSheet(0)获取第一个区域
try{//2. 连接数据库的几行代码
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();while(resultSet.next())
{
exce=newexcelTest();
exce.setId(resultSet.getInt("id"));
exce.setName(resultSet.getString("name"));
exce.setNum(resultSet.getString("num"));
excelTests.add(exce);
}
sheet.addCell(new Label(0,0,"编号"));
sheet.addCell(new Label(1,0,"姓名"));
sheet.addCell(new Label(2,0,"学号"));for(int i = 0; i
sheet.addCell(new jxl.write.Number(0,i+1,excelTests.get(i).getId()));//Label对应数据库String类型数据
sheet.addCell(new Label(1,i+1,excelTests.get(i).getName()));
sheet.addCell(new Label(2,i+1,excelTests.get(i).getNum()));
}
wwb.write();
}catch(SQLException e) {
e.printStackTrace();
}finally{
wwb.close();
}
}
}