導航:首頁 > 編程語言 > java讀取合並單元格

java讀取合並單元格

發布時間:2022-08-08 15:24:10

java excel 怎麼合並大的單元格

POIexcel表格何合並單元格

Java代碼
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;

public class ExcelTest {

/**
* @param args
*/
public static void main(String[] args) throws IOException {

try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 式象

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);

sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 文處理
ce.setCellValue("項目\\期"); // 表格第行第列顯示數據
ce.setCellStyle(style); // 式居
int num = 0;
for (int i = 0; i < 9; i++) { // 循環9每都要跨單元格顯示
// 計算單元格跨格
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 單元格合並
// 四參數別:起始行起始列結束行結束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨單元格顯示數據
cell.setCellStyle(style); // 式
// 跨單元格顯示數據:兩行行別兩格格行兩格數量金額
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("數量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金額");
cell2.setCellStyle(style);
num++;
}

// 面加合計百比

// 合計 加要跨單元格
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("合計");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("數量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金額");
cell2.setCellStyle(style);

// 百比 同
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb.setCellValue("百比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb1.setCellValue("數量");
cellb1.setCellStyle(style);
cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("金額");
cellb2.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}

}

}

② java poi 怎麼讀取Excel中合並單元格的值,我讀取合並單元格的第一個格有值,其他的都是空。

/**
* 獲取合並單元格的值
* @param sheet
* @param row
* @param column
* @return
*/
public String getMergedRegionValue(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();

for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress ca = sheet.getMergedRegion(i);
int firstColumn = ca.getFirstColumn();
int lastColumn = ca.getLastColumn();
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();

if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
Row fRow = sheet.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);

return getCellValue(fCell);
}
}
}

return null;
}

③ java poi怎麼讀取Excel中合並單元格的值

獲取合並單元格的值 @param sheet @param row @param column @return。

④ java怎麼合並excel單元格

有很多格式完全一樣的Excel文件,只是每個Excel裡面有很過工作薄,而這些工作薄的數據格式都相同,只是數據內容不同。

我想實現的是這樣的Excel有很多,我就是想把這些Excle裡面的所以數據都匯總在一個大的Excel裡面,如果工作薄的名字相同,那麼就在這同一個工作薄裡面累數據就行....

閱讀全文

與java讀取合並單元格相關的資料

熱點內容
海南省分布式伺服器雲主機 瀏覽:31
世紀江湖聊天室源碼 瀏覽:247
阿里網盤的文件如何解壓 瀏覽:781
簡單淘寶客源碼 瀏覽:680
煎餅解壓視頻教程全集 瀏覽:339
平行線pdf 瀏覽:631
android鎖屏不退出程序運行 瀏覽:635
sap連接的伺服器地址是 瀏覽:426
linuxshell腳本從入門到精通 瀏覽:725
python進制均值 瀏覽:629
pdfformac 瀏覽:318
用虛擬伺服器是什麼目的 瀏覽:192
壓縮機阿里巴巴 瀏覽:637
主圖指標源碼回踩 瀏覽:161
怎麼驗證伺服器埠 瀏覽:612
如何添加密碼卡 瀏覽:671
2021好聲音在哪個app觀看 瀏覽:126
壓縮層計算深度 瀏覽:391
愛奇藝怎麼不能源碼輸出 瀏覽:834
小孩視力訓練app哪個好 瀏覽:831