第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

垂直合并單元格并將數(shù)據(jù)插入單元格

垂直合并單元格并將數(shù)據(jù)插入單元格

UYOU 2022-05-25 15:45:11
我的問題是我有 4 個值。我需要在一個單元格中表示它們。我需要垂直合并單元格(列)并將四個值呈現(xiàn)在另一個下方(從上到下),在合并的單元格之間有換行符。我能夠垂直合并單元格,但無法在單個單元格中顯示四個值。CellRangeAddress cellRangeAddress = new CellRangeAddress(2,5,5,5);sheet.addMergedRegion(cellRangeAddress);
查看完整描述

2 回答

?
SMILET

TA貢獻1796條經(jīng)驗 獲得超4個贊

合并的單元格區(qū)域將它們定向到該區(qū)域中的第一個單元格。這意味著單元格樣式以及單元格值。所以首先需要設置第一個單元格的單元格樣式來換行。然后我們需要將所有單元格值連接到第一個單元格的單元格值中,該單元格值由換行符“\n”分隔。然后我們可以合并單元格。

例子:

樣品.xlsx:

http://img1.sycdn.imooc.com//628ddea40001c70603920250.jpg

代碼:


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

import org.apache.poi.ss.util.*;

import org.apache.poi.util.LocaleUtil;


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Locale;


class ExcelConcatenateAndMerge {


 private static void concatenateAndMerge(

  Sheet sheet, CellRangeAddress cellRangeAddress, DataFormatter formatter, FormulaEvaluator evaluator, CellStyle cellStyle) {


  Row row = null;

  Cell cell = null;

  Cell firstCell = null;

  String cellValue = "";

  boolean first = true;

  for (CellAddress cellAddress : cellRangeAddress) {

   row = sheet.getRow(cellAddress.getRow());

   if (first) {

    if (row == null) row = sheet.createRow(cellAddress.getRow());

    firstCell = row.getCell(cellAddress.getColumn());

    if (firstCell == null) firstCell = row.createCell(cellAddress.getColumn());

    firstCell.setCellStyle(cellStyle);

    cellValue = formatter.formatCellValue(firstCell, evaluator);

    first = false;

   } else {

    if (row != null) {

     cell = row.getCell(cellAddress.getColumn());

     if (cell != null) {

      cellValue += "\n" + formatter.formatCellValue(cell, evaluator);

     } else cellValue += "\n" + "";

    } else cellValue += "\n" + "";

   }

  }


  firstCell.setCellValue(cellValue);


  sheet.addMergedRegion(cellRangeAddress);


 }


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


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


  Locale locale = new Locale("en", "US");

  LocaleUtil.setUserLocale(locale);

  DataFormatter formatter = new DataFormatter();

  FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

  CellStyle cellStyle = wb.createCellStyle();

  cellStyle.setWrapText(true);


  Sheet sheet = wb.getSheetAt(0);

  CellRangeAddress cellRangeAddress = new CellRangeAddress(2,5,5,5);


  concatenateAndMerge(sheet, cellRangeAddress, formatter, evaluator, cellStyle);


  FileOutputStream out = new FileOutputStream("SAMPLENEW.xlsx");

  wb.write(out);

  out.close();

  wb.close();


 }

}

SAMPLENEW.xlsx: 

http://img1.sycdn.imooc.com//628ddebb000114de03720251.jpg

concatenateAndMerge使用DataFormatter的方法是因為源單元格內容可能不僅僅是文本,而是日期或其他數(shù)字。由于合并的單元格內容需要是串聯(lián)字符串,因此之前單個單元格的不同內容應該出現(xiàn)在該串聯(lián)字符串中,如之前在單個單元格中所示。這就是為什么DataFormatter. 我的方法使用FormulaEvaluator是因為源單元格內容也可能包含公式。



查看完整回答
反對 回復 2022-05-25
?
牛魔王的故事

TA貢獻1830條經(jīng)驗 獲得超3個贊

如果直接在 excel 中使用 VBA 是一種可行的替代方案,您可以編寫一個宏來完成此任務。宏將保存在實際的 excel 文件中。


當 Excel 合并單元格時,它只保留第一個單元格的值,因此在合并這些單元格之前,請以您想要的格式連接它們的值。要為您的值添加換行符,請Chr(10)在您想要新行的位置連接。


請記住,如果您以這種方式格式化數(shù)字單元格,則單元格值將變?yōu)樽址虼瞬荒茉儆米鞴街械臄?shù)字。


以下代碼將執(zhí)行您想要的*:


Dim finalValue As String

For Each c In Selection

    finalValue = finalValue & IIf((Len(finalValue) > 0) And (Len(c.Text) > 0), Chr(10), Empty) & c.Text

Next c


Selection.Clear

Selection.Merge

Selection.Value = finalValue

*注意:如果您需要對可變單元格編號執(zhí)行此任務,我選擇了代碼,但它適用于任何Range對象


另請注意,除非您刪除And (Len(c.Text) > 0). IIf但是通過這樣做,將選擇與已合并的單元格合并將創(chuàng)建盡可能多的空行[the size of the merged cell] - 1


查看完整回答
反對 回復 2022-05-25
  • 2 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號