- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我正在尝试使用 HSSFWorkbook 将 Jtable 数据导出到 Excel 工作表中。我得到了表中的所有内容,但没有得到表标题,请任何人提供同样的帮助。
这里是用于获取 Jtable 内容的命令。
try {
HSSFWorkbook fWorkbook = new HSSFWorkbook();
HSSFSheet fSheet = fWorkbook.createSheet("new Sheet");
HSSFFont sheetTitleFont = fWorkbook.createFont();
File file = new File("/home/kishan/NetBeansProjects/JavaChecking/src/com/verve/SwingChecking/book.xls");
HSSFCellStyle cellStyle = fWorkbook.createCellStyle();
sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//sheetTitleFont.setColor();
TableModel model = jTable1.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
HSSFRow fRow = fSheet.createRow((short) i);
for (int j = 0; j < model.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(model.getValueAt(i, j).toString());
cell.setCellStyle(cellStyle);
}
}
FileOutputStream fileOutputStream;
fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
fWorkbook.write(bos);
bos.close();
fileOutputStream.close();
}catch(Exception e){
}
for (int i = 0; i < model.getColumnCount(); i++) {
HSSFRow fRow = fSheet.createRow((short) i);
for(int j = 0; j < model.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(model.getValueAt(i, j).toString());
System.out.println(model.getColumnName(j));
}
}
最后一个for循环没有添加表头数据。
我得到了这个 Excel 文件
如何同时获取表头?
最佳答案
这是我根据本线程中的答案实现的 HSSF 工作簿。
我创建了一个类ExcelWriter
,然后创建了一个带有两个参数的方法编写器;要使用的 JTable
和 FileLocation
。
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
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;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author oluwajayi
*/
public class ExcelWriter {
public static void Writer (JTable jTable1, String Location) throws FileNotFoundException, IOException {
HSSFWorkbook fWorkbook = new HSSFWorkbook();
HSSFSheet fSheet = fWorkbook.createSheet("new Sheet");
HSSFFont sheetTitleFont = fWorkbook.createFont();
HSSFCellStyle cellStyle = fWorkbook.createCellStyle();
sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//sheetTitleFont.setColor();
TableModel model = jTable1.getModel();
//Get Header
TableColumnModel tcm = jTable1.getColumnModel();
HSSFRow hRow = fSheet.createRow((short) 0);
for(int j = 0; j < tcm.getColumnCount(); j++) {
HSSFCell cell = hRow.createCell((short) j);
cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());
cell.setCellStyle(cellStyle);
}
//Get Other details
for (int i = 0; i < model.getRowCount(); i++) {
HSSFRow fRow = fSheet.createRow((short) i+1);
for (int j = 0; j < model.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(model.getValueAt(i, j).toString());
cell.setCellStyle(cellStyle);
}
}
FileOutputStream fileOutputStream;
fileOutputStream = new FileOutputStream(Location);
try (BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)) {
fWorkbook.write(bos);
}
fileOutputStream.close();
}
}
关于java - 使用 HSSFWorkbook 将 Jtable 导出到 Excelsheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21931113/
5i 需要将 HSSFWorkbook(Apache 的 POI)转换为 ByteArray,然后将 ByteArray 转换回 HSSFWorkbook。以下测试用例说明了我的问题: @Tes
` class A { private HSSFWorkbook workbook; public A() { workbook = new HSSFWorkboo
我想使用 Apache Poi 在 Excel 工作簿中插入图像,但无法设置图像大小。我写了这段代码,但这不是我想要的。 InputStream inputStream2=new FileIn
我想将电子表格中标题行的字体设置为粗体。我能够使用以下代码在我的主要功能中做到这一点: HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet da
如何获取 Apache 的 POI HSSFWorkbook 的文件大小? 我有这样的代码 // inside Servlet OutputStream out = response.getOutpu
我希望我的 Web 应用程序用户将一些数据下载为 Excel 文件。 我有下一个函数在响应对象中发送输入流。 public static void sendFile(InputStream is, H
我正在使用 Spring 3 AbstractExcelView使用导出到 Excel 功能将文件下载到 .xls格式,但当我的行大于 65536 时,它会引发异常: Invalid row numb
我正在尝试从 mysql 获取数据并将其写入 Excel,条件如下所示, List getdata = reports.getReportData(); //This will return dat
调用简单 toBytes()确实会产生字节,但 exel 会引发警告。 Lost Document information 谷歌搜索给了我这个link看着Javadocs用于工作表和 POI HOW-
我正在尝试使用 Apache POI 3.17 创建一个带有预选特定单元格的旧式 Excel 文档 (HSSFWorkbook)。下面的代码真的很难看(它使用了反射和私有(private)字段)但是完
是否有一种方法可以在 JSP 页面中显示 HSSFWorkbook 对象,而无需在服务器端输出文件?在下面的代码中,我提供了模板文件和 net.sf.jxls.transformer.XLSTrans
我正在打开一个有 3 个选项卡的 Excel 工作表,我将 i 写入这 2 个选项卡取决于在表单中单击给定的三个链接中的哪一个。为了进一步解释,我在“添加”、“取消”、“修改”表单中有 3 个链接,单
我正在使用 HSSFWorkbook,我想知道应该在哪里关闭构建 HSSFWorkbook 对象时使用的 InputStream 对象。 HSSFWorkbook 是否在其构造函数本身中关闭流?或者我
我希望将 HSSFWorkbook 转换为 CSV 文件格式,然后将其写入 ServletOutputStream,其中我将 setContentType 设置为 text/x-csv . 提前致谢。
我想创建一个 HSSFCellSyle 并将其复制到文本满足某些特定条件的单元格。 目前,我使用一种方法在每次单元格满足条件时创建新的 HSSFCellSyle,尽管样式参数是相同的。 此外,由于
我使用poi-3.2-FINAL-20081019.jar。错误: Type mismatch: cannot convert from HSSFWorkbook to Workbook try {
我必须为用户提供下载 Excel 的功能。 这是我的 Controller 代码片段 @RequestMapping(value = "downloadFIReport.do", method = R
您好,我正在尝试使用 HSSFWorkbook 将 Jtable 数据导出到 Excel 工作表中。我得到了表中的所有内容,但没有得到表标题,请任何人提供同样的帮助。 这里是用于获取 Jtable 内
我正在尝试将 EMF 图像插入 HSSFWorkbook(apache poi 3.9)。但是当我打开结果文档时 - 我发现内容错误。我正在寻找问题的解决方案,并在 apache 邮件上找到了这封信:
我对 HSSFWorkbook 对象有疑问。我有一个 XLS 模板,其中第一行是深灰色,接下来的行是浅灰色。在生成报告期间,我想将几个单元格的背景颜色(完美工作)更改为红色/蓝色。但是调用之后:
我是一名优秀的程序员,十分优秀!