java 匯出excel(複雜案例)

來源:互聯網
上載者:User

標籤:region   exce   class   []   try   匯出   system   imp   string   

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
*/
@SuppressWarnings("deprecation")
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, 2, (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();
}

}

}

======================================================

用到的jar包

poi-3.8.jar
poi-examples-3.8.jar
poi-excelant-3.8.jar
poi-ooxml-3.8.jar
poi-ooxml-schemas-3.8.jar
poi-scratchpad-3.8.jar

======================================================

效果

java 匯出excel(複雜案例)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.