代码如下
public static void excelLoginLog() {
SqlData db = null;
Workbook workbook = new HSSFWorkbook();
Sheet sheet = (Sheet) workbook.createSheet("Login Logs");
try {
//这些目前是写死的数据,动态数据可从数据库获取
String username = "用户一";
String truename = "用户王";
String mobile = "xxxxx";
String dept01 = "部门一";
String dept02 = "部门二";
String thelastdate = "2024-04-22";
String appcodeversion = "安卓001";
// db = new SqlData();
// String sql = ""//这里如果是取数据库的数据就写sql
// ResultSet rs = db.executeQuery(sql);
int rowNum = 0;
Row headerRow = ((org.apache.poi.ss.usermodel.Sheet) sheet).createRow(rowNum++);
// 创建加粗字体
Font boldFont = workbook.createFont();
boldFont.setBold(true);
boldFont.setFontHeightInPoints((short) 14); // 设置字体大小为14
// 创建加粗样式
CellStyle boldStyle = workbook.createCellStyle();
boldStyle.setFont(boldFont);
headerRow.createCell(0).setCellValue("用户名");
headerRow.createCell(1).setCellValue("真实姓名");
headerRow.createCell(2).setCellValue("手机号");
headerRow.createCell(3).setCellValue("部门1");
headerRow.createCell(4).setCellValue("部门2");
headerRow.createCell(5).setCellValue("最后登陆时间");
headerRow.createCell(6).setCellValue("app版本号");
// 设置加粗样式
for (Cell cell : headerRow) {
cell.setCellStyle(boldStyle);
}
// 设置列宽
sheet.setColumnWidth(2, 12 * 256); // 手机号列宽为11个字符的宽度
sheet.setColumnWidth(5, 21 * 256); // 最后登录时间列宽为21个字符的宽度
sheet.setColumnWidth(6, 13 * 256); // app版本号列宽为12个字符的宽度
sheet.setColumnWidth(0, 12 * 256); // 用户名列宽为12个字符的宽度
sheet.setColumnWidth(1, 12 * 256); // 真实姓名列宽为12个字符的宽度
// while (rs.next()) {
Row row = ((org.apache.poi.ss.usermodel.Sheet) sheet).createRow(rowNum++);
// String username = rs.getString(1);
// String truename = rs.getString(2);
// String mobile = rs.getString(3);
// String dept01 = rs.getString(4);
// String dept02 = rs.getString(5);
// String thelastdate = com.client.user.impl.UserImpl.getlastlogintimeNew(username, "", db);
// String appcode_cms = "1234";
// String sqlstr2 = "xxxxx" + username + "' and appid='" + appcode_cms + "'";
// ResultSet rs1 = db.executeQuery(sqlstr2);
// String appcodeversion = "";
// while (rs1.next()) {
// appcodeversion = rs1.getString("appcodeversion");
// }
// rs1.close();
// rs1 = null;
row.createCell(0).setCellValue(username);
row.createCell(1).setCellValue(truename);
row.createCell(2).setCellValue(mobile);
row.createCell(3).setCellValue(dept01);
row.createCell(4).setCellValue(dept02);
row.createCell(5).setCellValue(thelastdate);
row.createCell(6).setCellValue(appcodeversion);
// }
// rs.close();
// rs = null;
// String filePath = "D:/xxx/xx/导出.xlsx";//写死的存放地址
// FileOutputStream fileOut = new FileOutputStream(filePath);
// workbook.write(fileOut);
// fileOut.close();
// 以下是可选择存放地址,创建文件选择对话框
JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
fileChooser.setDialogTitle("选择文件保存位置");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// 弹出文件选择对话框
int userSelection = fileChooser.showSaveDialog(null);
if (userSelection == JFileChooser.APPROVE_OPTION) {
// 用户点击了保存按钮
File selectedFolder = fileChooser.getSelectedFile();
String filePath = selectedFolder.getAbsolutePath() + "/用户导出.xlsx";
// 将文件保存到用户选择的文件夹中
FileOutputStream fileOut = new FileOutputStream(filePath);
workbook.write(fileOut);
fileOut.close();
System.out.println("Excel文件已成功保存到:" + filePath);
} else if (userSelection == JFileChooser.CANCEL_OPTION) {
// 用户取消了操作
System.out.println("保存操作已取消");
} else if (userSelection == JFileChooser.ERROR_OPTION) {
// 发生了错误
System.out.println("发生了错误");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (workbook != null) {
workbook.close();
}
} catch (Exception e) {
e.printStackTrace();
}
if (db != null) {
db.dbclose();
}
}
}
对应的import
import java.io.File;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.client.db.SqlData;
导出样式