java 导出excel

package export.excel;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import export.util.DBUtil;
import export.util.RandowPassword;
import export.util.Util;

/**
* 账号分配
* @author nan
*
*/
public class CreateSimpleExcelToDisk {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws SQLException {

// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("导出账号一");
sheet.setColumnWidth(0, 2000);
sheet.setColumnWidth(1, 7500);
sheet.setColumnWidth(2, 3500);
sheet.setColumnWidth(3, 6500);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("序号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("团组");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("用户名");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("密码");
cell.setCellStyle(style);

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
Connection conn = null;
PreparedStatement psrt = null;
conn = DBUtil.getConn();
ResultSet rs = null;
String sql = "select t.teamname,u.username,u.password from T_TEAM t,T_USER u where t.userid = u.userid order by u.username";
//String sql = "select t.teamname,u.username,u.password from T_TEAM t,T_USER u where t.userid = u.userid and u.username like 'GXJVJT'";
System.out.println(sql);
psrt = conn.prepareStatement(sql);
rs = psrt.executeQuery();
int count = 1;
while(rs.next()){
//System.out.println(rs.getString("teamname")+"--"+rs.getString("username")+"--"+rs.getString("password"));
//1 产生新密码
String password = RandowPassword.getRandomString(6);
System.out.println(password);
String newPassword = Util.md5(password);
String updateSql = "update T_USER u set u.password = "+"'"+newPassword+"'"+" where u.username like "+"'"+rs.getString("username")+"'";
System.out.println(updateSql);
psrt = conn.prepareStatement(updateSql);
int i = psrt.executeUpdate();
// 第四步,创建单元格,并设置值
if(i>0){
System.out.println("更新成功");
row = sheet.createRow((int) count );
row.createCell(0).setCellValue((double)count);
row.createCell(1).setCellValue(rs.getString("teamname"));
row.createCell(2).setCellValue(rs.getString("username"));
row.createCell(3).setCellValue(password);
//cell = row.createCell((short) 3);
}else{
System.out.println("更新失败");
}
System.out.println(count);
psrt.close();
count ++;
}
// 第六步,将文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream("E:/账号导出.xls");
wb.write(fout);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void closeCon(PreparedStatement ps,Connection conn) throws SQLException{
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

annan211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值