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

熱點內容
linuxtcp請求 瀏覽:259
pdcp加密范圍 瀏覽:668
6p照片加密 瀏覽:362
安卓桌面快捷鍵怎麼刪除 瀏覽:792
成都麻將2元加點演算法 瀏覽:663
股票每天高點低點演算法 瀏覽:20
使用路由器設置伺服器地址 瀏覽:274
matlab求逆矩陣的演算法 瀏覽:153
javaweburl 瀏覽:606
dota命令大全單機 瀏覽:711
android44屏蔽home鍵 瀏覽:886
五金程序員編程多少錢一個月 瀏覽:177
雲伺服器違規對外攻擊 瀏覽:188
phprips 瀏覽:6
python怎麼單干 瀏覽:385
創維安卓投屏為什麼沒聲音 瀏覽:750
餓了么app掃碼在哪裡 瀏覽:571
3600解壓慢 瀏覽:620
csgo單機命令 瀏覽:799
中間件裝飾器python 瀏覽:823