如何在 XHTML 页面中显示 Excel 单元格内容及其样式? [英] How to display the Excel Cell content along with its styling in XHTML page?

查看:37
本文介绍了如何在 XHTML 页面中显示 Excel 单元格内容及其样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JSF、Primefaces 和 XHTML 开发 Java Web 应用程序.

其中,我尝试使用 POIExcel 中读取 Cell 内容.在单元格中,它包含一些样式(粗体、颜色、线通过、下划线等)以及值.

现在,我需要在 XHTML 页面中显示值以及单元格的所有样式.

请帮我解决这个问题.

解决方案

该样式要么应用于整个单元格,要么应用于富文本字符串内容中的部分单元格内容.

如果应用于整个单元格,单元格样式有一个 字体 应用,您可以从中获取样式.

要从富文本字符串内容中获取样式,您需要获取 RichTextString 来自单元格.这包括多个格式化运行,每个都具有应用了 Font 的样式.因此,您需要遍历所有格式化运行以获取它们的样式和 Font.

示例:

import org.apache.poi.ss.usermodel.*;导入 org.apache.poi.xssf.usermodel.*;导入 java.io.FileInputStream;类 ReadExcelRichTextCells {静态 StringBuffer getHTMLFormatted(String textpart, Font font) {StringBuffer htmlstring = new StringBuffer();boolean wasbold = false;boolean wasitalic = false;boolean wasunderlined = false;boolean wassub = false;boolean wassup = false;如果(字体!= null){如果(font.getBold()){htmlstring.append("");wasbold = true;}如果(font.getItalic()){htmlstring.append("");wasitalic = true;}如果(font.getUnderline()== Font.U_SINGLE){htmlstring.append("");下划线=真;}if (font.getTypeOffset() == Font.SS_SUB) {htmlstring.append("");wassub = true;}如果(font.getTypeOffset()== Font.SS_SUPER){htmlstring.append("");wassup = 真;}}htmlstring.append(textpart);如果(浪费){htmlstring.append("</sup>");}如果(wassub){htmlstring.append("</sub>");}如果(带下划线){htmlstring.append("</u>");}如果(斜体){htmlstring.append("</i>");}如果(粗体){htmlstring.append("</b>");}返回 html 字符串;}public static void main(String[] args) 抛出异常 {工作簿 wb = WorkbookFactory.create(new FileInputStream("ExcelRichTextCells.xlsx"));Sheet sheet = wb.getSheetAt(0);for(行行:工作表){对于(单元格单元格:行){开关(cell.getCellTypeEnum()){case STRING://CellType 字符串XSSFRichTextString richtextstring = (XSSFRichTextString)cell.getRichStringCellValue();String textstring = richtextstring.getString();StringBuffer htmlstring = new StringBuffer();如果 (richtextstring.hasFormatting()) {for (int i = 0; i 

<小时>

此代码已使用 apache poi 3.17 进行测试.要将此代码与 apache poi 4.0.1 一起使用,请使用 CellStyle.getCellType 而不是 getCellTypeEnumCellStyle.getFontIndexAsInt而不是 getFontIndex.

<预><代码>...//开关(cell.getCellTypeEnum()){开关(cell.getCellType()){...//Font font = wb.getFontAt(cell.getCellStyle().getFontIndex());字体字体 = wb.getFontAt(cell.getCellStyle().getFontIndexAsInt());...//if (font == null) font = wb.getFontAt(cell.getCellStyle().getFontIndex());if (font == null) font = wb.getFontAt(cell.getCellStyle().getFontIndexAsInt());...

I am developing a Java Web Application using JSF, Primefaces and XHTML.

In which, I am trying to read the Cell content from Excel using POI. In cell, it contains some styling like (bold, color, line-through, underline and etc) along with the value.

So now, I need to show the value as well all the styles of the cell in XHTML page.

Kindly help me to solve this.

解决方案

The style is either applied to the whole cell or to parts of the cell content in rich text string content.

If applied to the whole cell, the cell style has a Font applied from which you can get the style.

For getting the styles from rich text string content, you need to get the RichTextString from the cell. This consists of multiple formatting runs, each having a style having a Font applied. So you need looping over all formatting runs to get their styles and their Fonts.

Example:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;

class ReadExcelRichTextCells {

 static StringBuffer getHTMLFormatted(String textpart, Font font) {

  StringBuffer htmlstring = new StringBuffer();

  boolean wasbold = false;
  boolean wasitalic = false;
  boolean wasunderlined = false;
  boolean wassub = false;
  boolean wassup = false;

  if (font != null) {
   if (font.getBold() ) {
    htmlstring.append("<b>");
    wasbold = true;
   }
   if (font.getItalic()) {
    htmlstring.append("<i>");
    wasitalic = true;
   }
   if (font.getUnderline() == Font.U_SINGLE) {
    htmlstring.append("<u>");
    wasunderlined = true;
   }
   if (font.getTypeOffset() == Font.SS_SUB) {
    htmlstring.append("<sub>");
    wassub = true;
   }
   if (font.getTypeOffset() == Font.SS_SUPER) {
    htmlstring.append("<sup>");
    wassup = true;
   }
  } 

  htmlstring.append(textpart);

  if (wassup) {
   htmlstring.append("</sup>");
  }
  if (wassub) {
   htmlstring.append("</sub>");
  }
  if (wasunderlined) {
   htmlstring.append("</u>");
  }
  if (wasitalic) {
   htmlstring.append("</i>");
  }
  if (wasbold) {
   htmlstring.append("</b>");
  }
  return htmlstring;  
 }

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

  Workbook wb  = WorkbookFactory.create(new FileInputStream("ExcelRichTextCells.xlsx"));

  Sheet sheet = wb.getSheetAt(0);
  for (Row row : sheet) {
   for (Cell cell : row) {
    switch (cell.getCellTypeEnum()) {
     case STRING: //CellType String
      XSSFRichTextString richtextstring = (XSSFRichTextString)cell.getRichStringCellValue();

      String textstring = richtextstring.getString();

      StringBuffer htmlstring = new StringBuffer();

      if (richtextstring.hasFormatting()) {
       for (int i = 0; i < richtextstring.numFormattingRuns(); i++) {
        int indexofformattingrun = richtextstring.getIndexOfFormattingRun(i);
        String textpart = textstring.substring(indexofformattingrun, 
                                               indexofformattingrun + richtextstring.getLengthOfFormattingRun(i));
        Font font = richtextstring.getFontOfFormattingRun(i);
        // font might be null if no formatting is applied to the specified text run
        // then font of the cell should be used.
        if (font == null) font = wb.getFontAt(cell.getCellStyle().getFontIndex());
        htmlstring.append(getHTMLFormatted(textpart, font));
       }
      } else {
       Font font = wb.getFontAt(cell.getCellStyle().getFontIndex());
       htmlstring.append(getHTMLFormatted(textstring, font));
      } 

      System.out.println(htmlstring);
      break;

     //case ... other CellTypes

     default:
      System.out.println("default cell"); //should never occur
    }
   }
  }

  wb.close();

 }
}


This code was tested using apache poi 3.17. For using this code with apache poi 4.0.1 do using CellStyle.getCellType instead of getCellTypeEnumand CellStyle.getFontIndexAsInt instead of getFontIndex.

...
//switch (cell.getCellTypeEnum()) {
switch (cell.getCellType()) {
...
//Font font = wb.getFontAt(cell.getCellStyle().getFontIndex());
Font font = wb.getFontAt(cell.getCellStyle().getFontIndexAsInt());
...
//if (font == null) font = wb.getFontAt(cell.getCellStyle().getFontIndex());
if (font == null) font = wb.getFontAt(cell.getCellStyle().getFontIndexAsInt());
...

这篇关于如何在 XHTML 页面中显示 Excel 单元格内容及其样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆