セル幅よりも大きいセル値をマージしたApache-POIを使用してExcel行の高さを増やすにはどうすればよいですか?

FREEVARUN

私はJavaクラスを使用して大きなExcelを作成しています。Excelには、文字列を格納する結合セルが含まれています。文字列の長さが非常に長いので、この文字列を動的に取得しています。完全な文字列がそのセルに収まるように、結合されたセルの高さを増やす必要があります。「テキストの折り返し」を使用してみました。テキストを折り返しますが、Excelで完全な文字列が表示されないため、結合されたセルの高さが増加しません。私が使用しているJavaクラスは次のとおりです。

  1. XSSFWorkbook
  2. XSSFシート
  3. XSSFRow
  4. XSSFCell
  5. XSSFCellStype

そして他の必要な従属クラス。セルの値に従ってマージセルの高さを増やす方法はありますか?

アクセルリヒター

必要な行の高さを計算する方法の課題のアイデアを提供します。

Sheet.getColumnWidth(int)を使用して、列の文字幅で列幅を取得できますただし、これは数字のグリフ(1、2、3、4、5、6、7、8、9、0)のみを対象としているため、実際には正確ではありません。True Typeフォントには、必要な幅がはるかに少ないグリフ(。、|、l、...)があります。したがって、この結果は、テキストで使用されている幅の狭いグリフと同じくらい不正確になります。

charsの列幅を係数で修正する場合があります。私は5/4を使用します。しかし、これは使用される言語に大きく依存します。

次に、必要な行数を計算し、1行に使用されるデフォルトの行の高さを取得して必要な行の高さを取得し、それに必要な行数を掛けます。

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

public class CreateExcelCellWrapText {

 public static void main(String[] args) throws Exception {
  XSSFWorkbook workbook = new XSSFWorkbook();

  CellStyle cellstyle = workbook.createCellStyle();
  cellstyle.setWrapText(true);

  Sheet sheet = workbook.createSheet();

//__________________________not merged = height is auto sized

  Row row = sheet.createRow(0);

  Cell cell = row.createCell(2);
  cell.setCellValue("String cell content\nhaving line wrap.");
  cell.setCellStyle(cellstyle);

//__________________________merged = height is not auto sized

  row = sheet.createRow(2);

  cell = row.createCell(2);
  cell.setCellValue("String cell content\nhaving line wrap.");
  cell.setCellStyle(cellstyle);

  CellRangeAddress cellRangeAddress = new CellRangeAddress(2, 2, 2, 3);
  sheet.addMergedRegion(cellRangeAddress);

//__________________________merged with calculated height

  row = sheet.createRow(4);

  String text = "String cell content\nhaving line wrap.\nIt has new line marks and then a long text without new line marks.\nFollowed by short text part.\n\n\nGreetings";

  cell = row.createCell(2);
  cell.setCellValue(text);
  cell.setCellStyle(cellstyle);

  cellRangeAddress = new CellRangeAddress(4, 4, 2, 3);
  sheet.addMergedRegion(cellRangeAddress);

  //get the column width in character widths for the merged columns
  //this is not really accurate since it only is for number glyphs: 1,2,3,4,5,6,7,8,9,0
  //in true type fonts there are glyps (., |, l, ...) which need much less width
  int colwidthinchars = (sheet.getColumnWidth(2) + sheet.getColumnWidth(3)) / 256;
System.out.println(colwidthinchars);
  //correct the colwidthinchars by a factor 5/4 (highly dependent on the language used)
  colwidthinchars = Math.round(colwidthinchars * 5f/4f);
System.out.println(colwidthinchars);

  //calculate the needed rows dependent on the text and the column width in character widths
  String[] chars = text.split("");
  int neededrows = 1;
  int counter = 0;
  for (int i = 0; i < chars.length; i++) {
   counter++;
   if (counter == colwidthinchars) {
System.out.println("new line because of charcter count");
    neededrows++;
    counter = 0;
   } else if ("\n".equals(chars[i])) {
System.out.println("new line because of new line mark");
    neededrows++;
    counter = 0;
   }
  }

System.out.println(neededrows);

  //get default row height
  float defaultrowheight = sheet.getDefaultRowHeightInPoints();
System.out.println(defaultrowheight);

  row.setHeightInPoints(neededrows * defaultrowheight);

  workbook.write(new FileOutputStream("CreateExcelCellWrapText.xlsx"));
  workbook.close();
 }
}

これは、デフォルトのフォントとフォントサイズが使用されるために機能します。異なるフォントと異なるフォントサイズが使用されると、課題は無限に増加します;-)。

Javaを使用して、定義された幅を持つ複数行のリッチテキストフィールド(任意のフォント、任意のフォントサイズ)の必要な高さを取得する方法を参照してくださいJTextPane希望の高さを取得するためにフォーマットされたテキストをaでレンダリングする例

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ