java导出EXCEL文件

工作需求,要求java代码写出获取的信息结果,并以EXCEL文档输出,保存起来。

当然需要大家下载POI的jar包。

这几个jar包有了,估计没太大问题了。

主程序是javafx方向的,不是fx的可以改一下

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


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


import com.sun.corba.se.spi.orbutil.fsm.Action;


import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

//下面贴出源码,如果有问题,大家稍微调整一下

public class TestExport implements EventHandler<ActionEvent>{
public void handle(ActionEvent event) {
//创建文档
HSSFWorkbook wb = new HSSFWorkbook();
//创建一个居中格式的文件名称  
HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置EXCEL下标名称
HSSFSheet sheet = wb.createSheet("测试报告");
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 5));
HSSFRow row1 = sheet.createRow((int)0);
HSSFCell cell1 = row1.createCell((short)0);
cell1.setCellValue("测 试 报 告");
cell1.setCellStyle(style);
//设置题目字体格式
HSSFFont font = wb.createFont();
        font.setFontName("仿宋_GB2312");
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        font.setFontHeightInPoints((short) 16);
        HSSFCellStyle style1 = wb.createCellStyle();
        style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style1.setFont(font);
        cell1.setCellStyle(style1);
//设置行宽
sheet.setColumnWidth((short) 0, (short)6000);
sheet.setColumnWidth((short) 1, (short)6000);
sheet.setColumnWidth((short) 2, (short)6000);
sheet.setColumnWidth((short) 3, (short)6000);
sheet.setColumnWidth((short) 4, (short)6000);
sheet.setColumnWidth((short) 5, (short)6000);
sheet.setColumnWidth((short) 6, (short)6000);
sheet.setColumnWidth((short) 7, (short)6000);
    //第一行横向信息
HSSFRow row = sheet.createRow((int)3);

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);
cell = row.createCell((short)4);
cell.setCellValue("网络延迟");
cell.setCellStyle(style);
cell = row.createCell((short)5);
cell.setCellValue("导出日期");
cell.setCellStyle(style);


//第二行横向信息
HSSFRow row2 = sheet.createRow((int)5);
HSSFCell cell2 = row2.createCell((short)0);
//从后台获取信息,填入对应的框内
TestchoiceController tcc = new TestchoiceController();

String comip = (tcc.tcpip).getText();//前台获取的对方IP地址
cell2.setCellValue(comip);
cell2.setCellStyle(style);
cell2 = row2.createCell((short)1);
cell2.setCellValue("是");
cell2.setCellStyle(style);
cell2 = row2.createCell((short)2);
cell2.setCellValue("正常");
cell2.setCellStyle(style);
cell2 = row2.createCell((short)3);
cell2.setCellValue("优秀");
cell2.setCellStyle(style);
cell2 = row2.createCell((short)4);
cell2.setCellValue("未知");
cell2.setCellStyle(style);
cell2 = row2.createCell((short)5);
//获取报告时间
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
String time = dateFormat.format(date);
cell2.setCellValue(time);
cell2.setCellStyle(style);
//写出到桌面创建快捷方式
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream("C:/Users/Administrator/Desktop/测试报告.xls");
System.out.println("导出成功");
wb.write(fileOut);
wb.close();
fileOut.close();
} catch (FileNotFoundException e) {
System.out.println("导出失败");
e.printStackTrace();
} catch (IOException e) {
System.out.println("导出失败");
e.printStackTrace();
}

}
}

可以根据我的代码,根据需求再调整,包括合并单元格的内容。

分割线:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

如果上面的不太好用,可以试试下面这段,是我修改过的。

public class TestExport implements EventHandler<ActionEvent>{
private String ipAddress="";//前台获取的对方IP地址
public TestExport(String tcpip){
ipAddress=tcpip;
}
public void handle(ActionEvent event) {
//创建文档
HSSFWorkbook wb = new HSSFWorkbook();
//创建一个居中格式的文件名称  
HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置EXCEL下标名称
HSSFSheet sheet = wb.createSheet("测试报告");
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 5));
HSSFRow row1 = sheet.createRow((int)0);
HSSFCell cell1 = row1.createCell(0);//
cell1.setCellValue("测 试 报 告");
cell1.setCellStyle(style);
//设置题目字体格式
HSSFFont font = wb.createFont();
        font.setFontName("仿宋_GB2312");
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        font.setFontHeightInPoints((short) 16);
        HSSFCellStyle style1 = wb.createCellStyle();
        style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style1.setFont(font);
        cell1.setCellStyle(style1);
//设置行宽
sheet.setColumnWidth((short) 0, (short)6000);
sheet.setColumnWidth((short) 1, (short)6000);
sheet.setColumnWidth((short) 2, (short)6000);
sheet.setColumnWidth((short) 3, (short)6000);
sheet.setColumnWidth((short) 4, (short)6000);
sheet.setColumnWidth((short) 5, (short)6000);
sheet.setColumnWidth((short) 6, (short)6000);
sheet.setColumnWidth((short) 7, (short)6000);
    //第一行横向信息
HSSFRow row = sheet.createRow((int)3);

HSSFCell cell = row.createCell(0);
cell.setCellValue("网络地址");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("是否连接");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("网络宽带");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("网络传输质量");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("网络延迟");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("导出日期");
cell.setCellStyle(style);


//第二行横向信息
HSSFRow row2 = sheet.createRow((int)5);
HSSFCell cell2 = row2.createCell(0);
//从后台获取信息,填入对应的框内


String comip = ipAddress;
cell2.setCellValue(comip);
cell2.setCellStyle(style);
cell2 = row2.createCell(1);
cell2.setCellValue("是");
cell2.setCellStyle(style);
cell2 = row2.createCell(2);
cell2.setCellValue("正常");
cell2.setCellStyle(style);
cell2 = row2.createCell(3);
cell2.setCellValue("优秀");
cell2.setCellStyle(style);
cell2 = row2.createCell(4);
cell2.setCellValue("未知");
cell2.setCellStyle(style);
cell2 = row2.createCell(5);
//获取报告时间
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
String time = dateFormat.format(date);
cell2.setCellValue(time);
cell2.setCellStyle(style);
//写出到桌面创建快捷方式
FileOutputStream fileOut;
try {
File pathReport=new File("C:\\testChoose");
if(!pathReport.exists()){
pathReport.mkdir();
}
File fileReport=new File("C:\\testChoose\\测试报告.xls");
if(!fileReport.exists()){
fileReport.createNewFile();
}
fileOut = new FileOutputStream(fileReport);
System.out.println("导出成功");
wb.write(fileOut);
wb.close();
fileOut.close();

Stage stage = new Stage();
Group root = new Group();
Scene scene = new Scene(root, 200, 130);
stage.setScene(scene);
stage.setResizable(false);
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("打开测试报告");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("xls", "*.xls"),new FileChooser.ExtensionFilter("Allfiles", "*.*"));
fileChooser.setInitialDirectory(new File("C:\\testChoose\\"));// System.getProperty("user.home")));
fileChooser.setInitialFileName("测试报告.xls");
File showOpenDialog = fileChooser.showOpenDialog(stage);
final Desktop desktop = Desktop.getDesktop();
if(showOpenDialog.isFile()){//判断选择的是否为文件
desktop.open(showOpenDialog);
}
} catch (FileNotFoundException e) {
System.out.println("导出失败");
e.printStackTrace();
} catch (IOException e) {
System.out.println("导出失败");
e.printStackTrace();
}

}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值