【问题标题】:Create excel file with table of contents Spring boot java使用目录Spring boot java创建excel文件
【发布时间】:2020-02-04 08:08:36
【问题描述】:

我在技术上知道如何使用工作表创建 xls。但是我在为我的 excel 中的每个工作表创建目录时遇到问题。

示例在此消息下方。 如您所见,有一个工作表名称和具有相应工作表名称的行(例如 Sheet1,Sheet2)。 还有一种可能的语法可以将 Sheet1 链接到 Sheet1 吗?

谢谢

【问题讨论】:

标签: java excel apache-poi


【解决方案1】:

您的屏幕截图显示的是HyperlinkType.DOCUMENT 类型的超链接。在https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ 中有一个HyperlinkExample.java。还展示了如何在同一工作簿中创建指向目标工作表和单元格的超链接。

要创建屏幕截图显示的内容,您还需要了解以下内容:

创建工作簿、工作表、行和单元格。设置单元格内容。设置自动筛选。设置列宽。

这一切都显示在Busy Developers' Guide to HSSF and XSSF Features 中。 https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ 和/或 https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/ 中的大部分内容的完整示例。

完全创建您的屏幕截图显示的示例:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

class CreateExcelLinkedSheetsTOC {

 public static void main(String[] args) throws Exception {

  try (
   Workbook workbook = new XSSFWorkbook(); 
   FileOutputStream fileout = new FileOutputStream("./Excel.xlsx")
   //Workbook workbook = new HSSFWorkbook(); 
   //FileOutputStream fileout = new FileOutputStream("./Excel.xls")
                                                                      ) {

   // we need creation helber for creating the links
   CreationHelper creationHelper = workbook.getCreationHelper();

   // hyperlink cell style
   CellStyle hlink_style = workbook.createCellStyle();
   Font hlink_font = workbook.createFont();
   hlink_font.setUnderline(Font.U_SINGLE);
   hlink_font.setColor(IndexedColors.BLUE.getIndex());
   hlink_style.setFont(hlink_font);

   // create 6 sheets
   Sheet sheet = null;
   for (int i = 1; i < 7; i++) {
    sheet = workbook.createSheet("Sheet" + i);
   }
   // create sheet "Source" as the 7th sheet
   sheet = workbook.createSheet("Source");

   // create sheet "Table of Contents"
   sheet = workbook.createSheet("Table of Contents");
   // unselect first sheet
   workbook.getSheetAt(0).setSelected(false);
   // make "Table of Contents" the new first sheet
   workbook.setSheetOrder("Table of Contents", 0);
   // make "Table of Contents" the active sheet
   workbook.setActiveSheet(0);
   // create header row
   Row row = sheet.createRow(0);
   Cell cell = row.createCell(1);
   cell.setCellValue("#");
   cell = row.createCell(2);
   cell.setCellValue("Sheet Name");

   // create content rows
   String sheetName = "";
   Hyperlink link = null;
   int numberOfSheets = workbook.getNumberOfSheets();
   for (int i = 1; i < numberOfSheets; i++) {
    row = sheet.createRow(i);
    cell = row.createCell(1);
    cell.setCellValue(i);
    cell = row.createCell(2);
    sheetName = workbook.getSheetName(i);
    cell.setCellValue(sheetName);
    // create hyperlink to cell A1 in sheet with sheetName
    link = creationHelper.createHyperlink(HyperlinkType.DOCUMENT);
    link.setAddress("'" + sheetName + "'!A1");
    cell.setHyperlink(link);
    // style the link cell
    cell.setCellStyle(hlink_style);
   }

   // set AutoFilter as shown in image
   sheet.setAutoFilter(new CellRangeAddress(
    0, numberOfSheets-1, 1, 2)
   );

   // set column widths
   sheet.setColumnWidth(0, 5 * 256);
   sheet.setColumnWidth(1, 5 * 256);
   sheet.setColumnWidth(2, 15 * 256);

   workbook.write(fileout);
  }
 }
}

【讨论】:

  • 我只是在使用来自 POJO 的不同值填充工作表时遇到问题。希望你能帮助我。
  • @Lep:请就此提出一个具体的新问题。请在该新问题中描述您的问题。你试过什么?你到底卡在哪里了?请以minimal reproducible example 的形式提供您尝试过的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
相关资源
最近更新 更多