導航:首頁 > 編程語言 > 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讀取合並單元格相關的資料

熱點內容
一堆文件夾怎麼弄出來 瀏覽:742
博途如何編譯硬體 瀏覽:418
fortran程序pdf 瀏覽:503
電池消耗演算法 瀏覽:393
伺服器中斷連接怎麼處理 瀏覽:221
上世紀互聯網不發達程序員很難 瀏覽:840
語音識別android開源 瀏覽:761
地埋式垃圾壓縮中轉站 瀏覽:901
apachehttpdlinux 瀏覽:943
快遞員中通app預付款是什麼 瀏覽:843
java路徑轉義 瀏覽:856
keytool加密演算法 瀏覽:130
笑臉圖案的APP相機是什麼軟體 瀏覽:249
app軟體為什麼會被下架 瀏覽:979
從內存到硬碟的命令是 瀏覽:51
程序員的爸爸們的發型 瀏覽:122
魔獸世界傷害壓縮是怎麼壓的 瀏覽:975
壓縮機型號hp 瀏覽:957
配音虛弱的程序員 瀏覽:61
8歲小學生程序員編程 瀏覽:255