excel导出功能及main方法测试调用

代码如下

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;

导出样式

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WAZYY0619

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

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

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

打赏作者

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

抵扣说明:

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

余额充值