1. 程式人生 > >關於POI設定SHEET名稱以及合併單元格,複製單元格方法

關於POI設定SHEET名稱以及合併單元格,複製單元格方法

//SHEET命名

	Workbook workbook = ReadExcel.openExcleFile(srcXlsxPath);

		// 獲取合同到期工作簿
		Sheet sheet1 = workbook.getSheetAt(0);// 獲取頁籤
		workbook.setSheetName(0, "12" + "月合同到期");

//合併單元格
sheet1.addMergedRegion(new CellRangeAddress(csize + 2, csize + 2, 0, 6));// 合併單元格
sheet1.addMergedRegion(new CellRangeAddress(2, csize + 2, 12, 12));// 合併單元格


//poi工具類

package com.bpms.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 
 * 讀取excle檔案內容
 * 
 * @author liuhh 2014/10/20.
 * 
 * @version V1.00.
 * 
 *         
 */
public class ReadExcel {

	/** 日誌. */
	public static Logger logger = Logger.getLogger(ReadExcel.class);

	/**
	 * 私有構造方法.
	 */
	private ReadExcel() {

	}

	/**
	 * 開啟excle檔案
	 * 
	 * @param fileName
	 *            帶字尾excle檔名稱
	 * @return
	 * @throws IOException
	 * @throws InvalidFormatException
	 */
	public static Workbook openExcleFile(String filePath) throws Exception {
		// 定義返回值
		Workbook workbook = null;
		try {
			// 開啟工作簿
			workbook = WorkbookFactory.create(new File(filePath));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		// 返回
		return workbook;
	}
	public static Workbook createworkbook(String filePath) throws IOException,InvalidFormatException {
		InputStream inp = new FileInputStream(filePath);
		if (!inp.markSupported()) {
		   inp = new PushbackInputStream(inp, 8);
		}
		if (POIFSFileSystem.hasPOIFSHeader(inp)) {
		  return new HSSFWorkbook(inp);
		}
		if (POIXMLDocument.hasOOXMLHeader(inp)) {
		  return new XSSFWorkbook(OPCPackage.open(inp));
		}
		throw new IllegalArgumentException("你的excel版本目前poi解析不了");
    }

	/**
	 * 
	 * @param cell
	 *            獲取的單元格
	 * @return 返回單元格中的值
	 */
	public static Object getCellValue(Cell cell) {
		// 定義返回值
		Object objResult = null;

		// 判斷單元格中的值
		if (cell != null) {
			// 匹配格式型別
			switch (cell.getCellType()) {

			// 字串型別
			case Cell.CELL_TYPE_STRING:

				objResult = cell.getRichStringCellValue().getString();

				break;
			// 貨幣型別
			case Cell.CELL_TYPE_NUMERIC:

				if (DateUtil.isCellDateFormatted(cell)) {
					objResult = cell.getDateCellValue();
				} else {
					objResult = cell.getNumericCellValue();
				}

				break;
			// 布林型別
			case Cell.CELL_TYPE_BOOLEAN:

				objResult = cell.getBooleanCellValue();

				break;
			// 公式
			case Cell.CELL_TYPE_FORMULA:
				try {
					objResult = cell.getNumericCellValue();
				} catch (IllegalStateException e) {
					objResult = String.valueOf(cell.getRichStringCellValue());
				}
			default:
			}
		}

		// 返回取到的單元格值
		return objResult;
	}

	/**
	 * 複製單元格
	 * 
	 * @param currentSheet
	 *            sheet頁
	 * @param startRow
	 *            開始行
	 * @param endRow
	 *            結束行
	 * @param pPosition
	 *            目標位置
	 */
	public static void copyRows(Sheet currentSheet, int startRow, int endRow,
			int pPosition) {

		int pStartRow = startRow - 1;
		int pEndRow = endRow - 1;
		int targetRowFrom;
		int targetRowTo;
		int columnCount;
		CellRangeAddress region = null;
		int i;
		int j;

		if (pStartRow == -1 || pEndRow == -1) {
			return;
		}

		for (i = 0; i < currentSheet.getNumMergedRegions(); i++) {
			region = currentSheet.getMergedRegion(i);
			if ((region.getFirstRow() >= pStartRow)
					&& (region.getLastRow() <= pEndRow)) {
				targetRowFrom = region.getFirstRow() - pStartRow + pPosition;
				targetRowTo = region.getLastRow() - pStartRow + pPosition;
				CellRangeAddress newRegion = region.copy();
				newRegion.setFirstRow(targetRowFrom);
				newRegion.setFirstColumn(region.getFirstColumn());
				newRegion.setLastRow(targetRowTo);
				newRegion.setLastColumn(region.getLastColumn());
				currentSheet.addMergedRegion(newRegion);
			}
		}

		for (i = pStartRow; i <= pEndRow; i++) {
			XSSFRow sourceRow = (XSSFRow) currentSheet.getRow(i);
			columnCount = sourceRow.getLastCellNum();
			if (sourceRow != null) {
				XSSFRow newRow = (XSSFRow) currentSheet.createRow(pPosition
						- pStartRow + i);
				newRow.setHeight(sourceRow.getHeight());
				for (j = 0; j < columnCount; j++) {
					XSSFCell templateCell = sourceRow.getCell(j);
					if (templateCell != null) {
						XSSFCell newCell = newRow.createCell(j);
						copyCell(templateCell, newCell);
					}
				}
			}
		}
	}

	public static void copyCell(XSSFCell srcCell, XSSFCell distCell) {
		distCell.setCellStyle(srcCell.getCellStyle());
		if (srcCell.getCellComment() != null) {
			distCell.setCellComment(srcCell.getCellComment());
		}
		int srcCellType = srcCell.getCellType();
		distCell.setCellType(srcCellType);
		if (srcCellType == XSSFCell.CELL_TYPE_NUMERIC) {
			if (HSSFDateUtil.isCellDateFormatted(srcCell)) {
				distCell.setCellValue(srcCell.getDateCellValue());
			} else {
				distCell.setCellValue(srcCell.getNumericCellValue());
			}
		} else if (srcCellType == XSSFCell.CELL_TYPE_STRING) {
			distCell.setCellValue(srcCell.getRichStringCellValue());
		} else if (srcCellType == XSSFCell.CELL_TYPE_BLANK) {
			// nothing21
		} else if (srcCellType == XSSFCell.CELL_TYPE_BOOLEAN) {
			distCell.setCellValue(srcCell.getBooleanCellValue());
		} else if (srcCellType == XSSFCell.CELL_TYPE_ERROR) {
			distCell.setCellErrorValue(srcCell.getErrorCellValue());
		} else if (srcCellType == XSSFCell.CELL_TYPE_FORMULA) {
			distCell.setCellFormula(srcCell.getCellFormula());
		} else { // nothing29

		}
	}
	
	public static void fileDownload(HttpServletResponse response,String path){  
	        //1.設定檔案ContentType型別,這樣設定,會自動判斷下載檔案型別  
	        response.setContentType("multipart/form-data");  
	        //2.設定檔案頭:最後一個引數是設定下載檔名(假如我們叫a.pdf)  
	        response.setHeader("Content-Disposition", "attachment;fileName="+"a.xlsx");  
	        ServletOutputStream out;  
	        //通過檔案路徑獲得File物件(假如此路徑中有一個download.pdf檔案)  
	        File file = new File(path);  
	        try {  
	            FileInputStream inputStream = new FileInputStream(file);  
	            //3.通過response獲取ServletOutputStream物件(out)  
	            out = response.getOutputStream();  
	            int b = 0;  
	            byte[] buffer = new byte[512];  
	            while (b != -1){  
	                b = inputStream.read(buffer);  
	                //4.寫到輸出流(out)中  
	                out.write(buffer,0,b);  
	            }  
	            inputStream.close();  
	            out.close();  
	            out.flush();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
   }  
}