java實(shí)現(xiàn)合并單元格的同時并導(dǎo)出excel示例
介紹
POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫的功能。POI可以操作的文檔格式有excel,word,powerpoint等,POI進(jìn)行跨行需要用到對象HSSFSheet對象,現(xiàn)在就當(dāng)我們程序已經(jīng)定義了一個HSSFSheet對象sheet。
跨第1行第1個到第2個單元格的操作為
sheet.addMergedRegion(new Region(0,(short)0,0,(short)1));
跨第1行第1個到第2行第1個單元格的操作為
sheet.addMergedRegion(new Region(0,(short)0,1,(short)0));
重點(diǎn)注意事項:
1.單元格CELL和ROW對象下標(biāo)都是從0開始的。
2.單元格合并時Region(1,2,3,4)第1個值的行號必須要比3位置的行號小,如果大于3就不能正常合并單元格
3.合并單元格的時候要合并的單單元格必須先創(chuàng)建,這樣方便后面再次獲取這個單元格來填充數(shù)據(jù),主要就是因為合并時不能由后向前進(jìn)行合并引起的。
示例代碼
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("項目\\日期"); // 表格的第一行第一列顯示的數(shù)據(jù)
ce.setCellStyle(style); // 樣式,居中
int num = 0;
for (int i = 0; i < 9; i++) { // 循環(huán)9次,每一次都要跨單元格顯示
// 計算從那個單元格跨到那一格
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 單元格合并
// 四個參數(shù)分別是:起始行,起始列,結(jié)束行,結(jié)束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨單元格顯示的數(shù)據(jù)
cell.setCellStyle(style); // 樣式
// 不跨單元格顯示的數(shù)據(jù),如:分兩行,上一行分別兩格為一格,下一行就為兩格,“數(shù)量”,“金額”
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("數(shù)量");
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("數(shù)量");
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("數(shù)量");
cellb1.setCellStyle(style);
cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("金額");
cellb2.setCellStyle(style);
/***這里是問題的關(guān)鍵,將這個工作簿寫入到一個流中就可以輸出相應(yīng)的名字,這里需要寫路徑就ok了。
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
**/
/**第二種是輸出到也面中的excel名稱
* pName="欄目統(tǒng)計表";
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment; filename="+new String(pName.getBytes("gb2312"),"ISO-8859-1")+".xls");
ServletOutputStream outStream=null;
try{
outStream = response.getOutputStream();
wb.write(outStream);
}catch(Exception e)
{
e.printStackTrace();
}finally{
outStream.close();
}
* */
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
java開發(fā)使用StringUtils.split避坑詳解
這篇文章主要為大家介紹了java開發(fā)使用StringUtils.split避坑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
利用consul在spring boot中實(shí)現(xiàn)分布式鎖場景分析
這篇文章通過場景分析給大家介紹如何利用consul在spring boot中實(shí)現(xiàn)簡單的分布式鎖功能,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-09-09
SpringBoot實(shí)現(xiàn)識別圖片中的身份證號與營業(yè)執(zhí)照信息
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何實(shí)現(xiàn)識別圖片中的身份證號與營業(yè)執(zhí)照信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-01-01
springboot項目中PropertySource如何讀取yaml配置文件
這篇文章主要介紹了springboot項目中PropertySource如何讀取yaml配置文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

