开发思路
开发思路解析
代码开发
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import com.ts.dao.SchoolMessageDao;
import com.ts.dao.TeacherMessageDao;
import com.ts.dao.TitleMessageDao;
import com.ts.dao.UserMessageDao;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class ExcelTest {
Workbook workbook = null;
Sheet sheet = null;
WritableWorkbook workbook1 = null;
ResultSet rs=null;
Connection con = null;
private JDBCutil dbutil = new JDBCutil();
SchoolMessageDao schoolMessageDao = new SchoolMessageDao();
public void exportSchoolExcel() {
try {
File file = new File("excel/school.xls");
workbook1 = Workbook.createWorkbook(file);
WritableSheet sheet= workbook1.createSheet("教务员信息", 0);
con = (Connection) dbutil.getCon();
rs=schoolMessageDao.ALLlist(con);
Label lab=new Label(0,0,"账号");
Label lab1=new Label(1,0,"姓名");
Label lab2=new Label(2,0,"性别");
Label lab3=new Label(3,0,"联系方式");
Label userid=null;
Label user=null;
Label sex=null;
Label contact=null;
sheet.addCell(lab);
sheet.addCell(lab1);
sheet.addCell(lab2);
sheet.addCell(lab3);
int row=1;
while(rs.next()) {
userid=new Label(0,row,rs.getString(1));
user=new Label(1,row,rs.getString(2));
sex=new Label(2,row,rs.getString(3));
contact=new Label(3,row,rs.getString(4));
sheet.addCell(userid);
sheet.addCell(user);
sheet.addCell(sex);
sheet.addCell(contact);
row++;
}
workbook1.write();
JOptionPane.showMessageDialog(null, "导出成功","系统提示",JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "导出失败","系统提示",JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
}finally {
try {
workbook1.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}