poiexcel:poi开发Excel的共通库来源: 发布时间:星期四, 2009年2月12日 浏览:27次 评论:0
package co.B2B.jouhou.common; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; 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; import org.apache.poi.po ![]() ![]() import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.math.BigDecimal; public ![]() private boolean bOpened; private HSSFWorkbook objWorkbook; private HSSFSheet objSheet; private ![]() ![]() ![]() ![]() public CommonExcel ![]() bOpened = false; objWorkbook = null; objSheet = null; } public HSSFWorkbook getWorkBook ![]() ![]() } /** * Excel Open * @param tempFileName (i/ ): */ public void open(String tempFileName) throws Exception { ![]() ![]() throw ![]() } ![]() FileInputStream fs = ![]() objWorkbook = ![]() objSheet = objWorkbook.getSheetAt(0); bOpened = true; } } /** * save file * @param fileName (i/ ): */ public void save(String fileName) throws Exception { ![]() ![]() FileOutputStream fs = ![]() objWorkbook.write(fs); fs.flush ![]() fs.close ![]() } ![]() throw ![]() } } /** * OutputStream * @param outputStream */ public void save(OutputStream outputStream) throws Exception { ![]() ![]() BufferedOutputStream out = ![]() objWorkbook.write(out); out.flush ![]() out.close ![]() } ![]() throw ![]() } } /** * closed * */ public void close ![]() ![]() ![]() objSheet = null; objWorkbook = null; bOpened = false; } ![]() throw ![]() } } /** * Edit sheet * @param index (i/ ): sheet no */ public void ![]() ![]() ![]() ![]() objSheet = objWorkbook.getSheetAt(index); } ![]() throw ![]() } } /** * * @param sheetName (i/ ): */ public void ![]() ![]() ![]() objSheet = objWorkbook.getSheet(sheetName); } ![]() throw ![]() } } /** * * @param index (i/ ): */ public void deleteSheet( ![]() ![]() ![]() objWorkbook.removeSheetAt(index); } ![]() throw ![]() } } /** * * @param index (i/ ): */ public void deleteSheet(String sheetName) throws Exception { ![]() ![]() this.deleteSheet(objWorkbook.getSheetIndex(sheetName)); } ![]() throw ![]() } } /** * * @param row (i/ ): row * @param col (i/ ): column * @ ![]() */ public String getCellName( ![]() ![]() String str1 = ""; String str2 = ""; String str = ""; String list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; ![]() ![]() str1 = list.sub ![]() ![]() str2 = list.sub ![]() } ![]() str2 = list.sub ![]() } row ![]() str = str1 + str2 + row; ![]() } /** * * @param cellName (i/ ): * @param row (/o): * @param col (/o): */ public void getCellRowCol(String cellName, ![]() ![]() String list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; ![]() ![]() ![]() rowcol[1] = list.indexOf(cellName.sub ![]() ![]() rowcol[0] = Integer.parseInt(cellName.sub ![]() } ![]() ![]() ![]() ![]() ![]() ![]() ![]() rowcol[1] = ((con1 + 1) * 26) + con2; rowcol[0] = Integer.parseInt(cellName.sub ![]() } } /** * * @param row (i/ ): * @param col (i/ ): * @param value (i/ ): */ public void ![]() ![]() ![]() throws Exception { ![]() ![]() HSSFRow objRow = objSheet.getRow(row); HSSFCell objCell = objRow.getCell(( ![]() objCell. ![]() objCell. ![]() } ![]() throw ![]() } } /** * * @param row (i/ ): * @param col (i/ ): * @param value (i/ ): */ public void ![]() ![]() ![]() throws Exception { ![]() ![]() HSSFRow objRow = objSheet.getRow(row); HSSFCell objCell = objRow.getCell(( ![]() objCell. ![]() } ![]() throw ![]() } } /** * * @param row (i/ ): * @param col (i/ ): * @param value (i/ ): */ public void ![]() ![]() ![]() throws Exception { ![]() ![]() ![]() } ![]() try { double tmpValue = ![]() ![]() ![]() } catch (Exception ex) { ![]() } } } /** * * @param row (i/ ): * @param col (i/ ): * @param value (i/ ): */ public void ![]() throws Exception { ![]() ![]() this.getCellRowCol(cellName, rc); } ![]() throw ![]() } ![]() ![]() ![]() } ![]() try { double tmpValue = ![]() ![]() ![]() } catch (Exception ex) { ![]() } } } /** * * @param sheetName (i/ ): */ public void ![]() throws Exception { ![]() ![]() objSheet = objWorkbook.getSheet(sheetName); objSheet. ![]() } ![]() throw ![]() } } /** * * @param cellName (i/ ): * @param value (i/ ): */ public void ![]() throws Exception { ![]() ![]() this.getCellRowCol(cellName, rc); this. ![]() } ![]() throw ![]() } } /** * * @param cellName (i/ ): * @param value (i/ ): */ public void ![]() throws Exception { ![]() ![]() this.getCellRowCol(cellName, rc); this. ![]() } ![]() throw ![]() } } private HSSFCellStyle cloneCellStyle(HSSFCellStyle objStyle1) { HSSFCellStyle objStyle2 = objWorkbook.createCellStyle ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() objStyle2. ![]() ![]() ![]() } /** * * @param row (i/ ): * @param col (i/ ): * @param clrIndex (i/ ): */ public void ![]() ![]() ![]() ![]() throws Exception { ![]() ![]() HSSFRow objRow = objSheet.getRow(row); HSSFCell objCell = objRow.getCell(( ![]() HSSFCellStyle objStyle = objCell.getCellStyle ![]() ![]() objStyle. ![]() objStyle. ![]() ![]() objCell. ![]() } ![]() throw ![]() } } /** * * @param row (i/ ): * @param col (i/ ): * @param clrIndex (i/ ): */ public void ![]() ![]() ![]() ![]() ![]() throws Exception { ![]() ![]() HSSFRow objRow = objSheet.getRow(row); HSSFCell objCell = objRow.getCell(( ![]() HSSFCellStyle objStyle = objCell.getCellStyle ![]() ![]() HSSFFont data_font = objWorkbook.createFont ![]() data_font. ![]() ![]() data_font. ![]() ![]() ![]() data_font. ![]() objStyle. ![]() objCell. ![]() } ![]() throw ![]() } } /** * * @param cellName (i/ ): * @param clrIndex (i/ ): */ public void ![]() ![]() throws Exception { ![]() ![]() this.getCellRowCol(cellName, rc); this. ![]() } ![]() throw ![]() } } /** * * @param cellName (i/ ): * @param clrIndex (i/ ): */ public void ![]() ![]() ![]() throws Exception { ![]() ![]() objSheet. ![]() ![]() ![]() } ![]() throw ![]() } } /** * * @param cellName (i/ ): * @param clrIndex (i/ ): */ public void ![]() ![]() throws Exception { ![]() ![]() objSheet.autoSizeColumn(( ![]() } ![]() throw ![]() } } /** * * @param cellName (i/ ): * @param clrIndex (i/ ): */ public void ![]() ![]() ![]() throws Exception { ![]() ![]() this.getCellRowCol(cellName, rc); this. ![]() } ![]() throw ![]() } } /** * * @param row (i/ ): * @param col (i/ ): * @param formula (i/ ): */ public void ![]() ![]() ![]() throws Exception { ![]() ![]() HSSFRow objRow = objSheet.getRow(row); HSSFCell objCell = objRow.getCell(( ![]() objCell. ![]() } ![]() throw ![]() } } /** * * @param cellName (i/ ): * @param formula (i/ ): */ public void ![]() throws Exception { ![]() ![]() this.getCellRowCol(cellName, rc); this. ![]() } ![]() throw ![]() } } /** * * @param row (i/ ): * @param col (i/ ): * @param link (i/ ): */ public void addCellLink( ![]() ![]() throws Exception { ![]() ![]() HSSFRow objRow = objSheet.getRow(row); HSSFCell objCell = objRow.getCell(( ![]() objCell. ![]() objCell.getStringCellValue ![]() } ![]() throw ![]() } } /** * * @param cellName (i/ ): * @param link (i/ ): */ public void addCellLink(String cellName, String link) throws Exception { ![]() ![]() this.getCellRowCol(cellName, rc); this.addCellLink(rc[0], rc[1], link); } ![]() throw ![]() } } public void copySheet(HSSFWorkbook originWorkBook, String pSourceSheetName,HSSFWorkbook targetWorkBook, String pTargetSheetName) { HSSFRow sourceRow = null; HSSFRow targetRow = null; HSSFCell sourceCell = null; HSSFCell targetCell = null; HSSFSheet sourceSheet = null; HSSFSheet targetSheet = null; Region region = null; ![]() ![]() ![]() sourceSheet = originWorkBook.getSheet(pSourceSheetName); targetSheet = targetWorkBook.getSheet(pTargetSheetName); pStartRow = sourceSheet.getFirstRowNum ![]() pEndRow = sourceSheet.getLastRowNum ![]() ![]() ![]() ![]() ![]() } for ( ![]() ![]() ![]() region = sourceSheet.getMergedRegionAt(i); ![]() ![]() ![]() ![]() ![]() ![]() ![]() region. ![]() region. ![]() targetSheet.addMergedRegion(region); } } for ( ![]() ![]() sourceRow = sourceSheet.getRow(i); ![]() ![]() continue; } targetRow = targetSheet.createRow(i - pStartRow + pPosition); targetRow. ![]() ![]() for ( ![]() ![]() ![]() ![]() ![]() sourceCell = sourceRow.getCell(j); ![]() ![]() continue; } targetCell = targetRow.createCell(j); targetCell. ![]() ![]() targetCell. ![]() ![]() ![]() ![]() targetCell. ![]() switch (cType) { ![]() targetCell. ![]() ![]() ![]() ![]() targetCell. ![]() ![]() ![]() ![]() targetCell. ![]() ![]() ![]() ![]() targetCell. ![]() ![]() ![]() ![]() targetCell. ![]() ![]() ![]() } } } } /** * * @param originSheetIndex (i/ ): * @param originLeftTopRow (i/ ): * @param originLeftTopCol (i/ ): * @param originRightBottomRow (i/ ): * @param originRightBottomCol (i/ ): * @param targetSheetIndex (i/ ): * @param targetLeftTopRow (i/ ): * @param targetLeftTopCol (i/ ): */ public void copyRange( ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() HSSFSheet sheet1 = objWorkbook.getSheetAt(originSheetIndex); HSSFSheet sheet2 = objWorkbook.getSheetAt(targetSheetIndex); for ( ![]() i ![]() HSSFRow row1 = sheet1.getRow(originLeftTopRow + i); ![]() ![]() continue; } HSSFRow row2 = sheet2.getRow(targetLeftTopRow + i); ![]() ![]() row2 = sheet2.createRow(targetLeftTopRow + i); row2. ![]() ![]() } for ( ![]() j ![]() HSSFCell cell1 = row1.getCell(( ![]() j)); ![]() ![]() continue; } HSSFCell cell2 = row2.getCell(( ![]() j)); ![]() ![]() cell2 = row2.createCell(( ![]() //sheet2. ![]() ![]() ![]() } cell2. ![]() ![]() cell2. ![]() ![]() ![]() ![]() ![]() ![]() cell2. ![]() ![]() } ![]() ![]() ![]() cell2. ![]() ![]() } } } for ( ![]() ![]() ![]() Region region1 = sheet1.getMergedRegionAt(i); ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() (LeftTopCol >= originLeftTopCol) && (RightBottomRow <= originRightBottomRow) && (RightBottomCol <= originRightBottomCol)) { LeftTopRow = targetLeftTopRow + (LeftTopRow - originLeftTopRow); LeftTopCol = targetLeftTopCol + (LeftTopCol - originLeftTopCol); RightBottomRow = targetLeftTopRow + (RightBottomRow - originLeftTopRow); RightBottomCol = targetLeftTopCol + (RightBottomCol - originLeftTopCol); sheet2.addMergedRegion( ![]() ![]() ( ![]() ![]() ( ![]() } } } ![]() throw ![]() } } /** * * @param originSheetName (i/ ): * @param originLeftTopCellName (i/ ): * @param originRightBottomCellName (i/ ): * @param targetSheetName (i/ ): * @param targetLeftTopCellName (i/ ): */ public void copyRange(String originSheetName, String originLeftTopCellName, String originRightBottomCellName, String targetSheetName, String targetLeftTopCellName) throws Exception { ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() originSheetIndex = objWorkbook.getSheetIndex(originSheetName); this.getCellRowCol(originLeftTopCellName, rc); originLeftTopRow = rc[0]; originLeftTopCol = rc[1]; this.getCellRowCol(originRightBottomCellName, rc); originRightBottomRow = rc[0]; originRightBottomCol = rc[1]; targetSheetIndex = objWorkbook.getSheetIndex(targetSheetName); this.getCellRowCol(targetLeftTopCellName, rc); targetLeftTopRow = rc[0]; targetLeftTopCol = rc[1]; this.copyRange(originSheetIndex, originLeftTopRow, originLeftTopCol, originRightBottomRow, originRightBottomCol, targetSheetIndex, targetLeftTopRow, targetLeftTopCol); } ![]() throw ![]() } } /** * * @param SheetName (i/ ): */ public void addSheet(String SheetName) throws Exception { ![]() ![]() objWorkbook.createSheet(SheetName); } ![]() throw ![]() } } /** * * @param leftTopRow (i/ ): * @param leftTopCol (i/ ): * @param rightBottomRow (i/ ): * @param rightBottomCol (i/ ): */ public void ![]() ![]() ![]() ![]() ![]() ![]() ![]() objSheet.addMergedRegion( ![]() ![]() ( ![]() ![]() ( ![]() } ![]() throw ![]() } } /** * * @param leftTopCellName (i/ ): * @param rightBottomCellName (i/ ): */ public void ![]() String rightBottomCellName) throws Exception { ![]() ![]() ![]() ![]() this.getCellRowCol(leftTopCellName, rc); lr = rc[0]; lc = rc[1]; this.getCellRowCol(rightBottomCellName, rc); this. ![]() } ![]() throw ![]() } } } 0
相关文章读者评论发表评论 |