Java项目之数据与表格的导出代码开发

开发思路

开发思路解析

代码开发

/*
*导出代码
*/
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");
			// 创建workbook
			workbook1 = Workbook.createWorkbook(file);
			// 创建sheet
			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) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, "导出失败","系统提示",JOptionPane.INFORMATION_MESSAGE);
			e.printStackTrace();
		}finally {
			try {
				workbook1.close();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值