123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- package com.jtgh.yjpt.common;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.math.BigDecimal;
- import java.sql.Timestamp;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.apache.commons.lang.StringUtils;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFDataFormat;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- import org.apache.poi.hssf.usermodel.HSSFFont;
- import org.apache.poi.hssf.usermodel.HSSFFooter;
- import org.apache.poi.hssf.usermodel.HSSFRichTextString;
- 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.ss.util.CellRangeAddress;
- import org.apache.poi.ss.util.NumberToTextConverter;
- public class PoiUtils {
- public static HSSFCell getCell(HSSFSheet sheet, int x, int y) {
- HSSFRow row = sheet.getRow(x - 1);
- if (row == null)
- return null;
- HSSFCell cell = row.getCell(y - 1);
- if (cell == null)
- return null;
- return cell;
- }
- /**
- * 取单元格内的数字,非数字的单元格返回0
- */
- public static double getNumericCellValue(HSSFCell cell) {
- if (cell == null)
- return 0;
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_STRING: {
- try {
- return Double.parseDouble(StringUtils.trim(cell
- .getStringCellValue()));
- } catch (NumberFormatException pe) {
- return 0;
- }
- }
- case HSSFCell.CELL_TYPE_NUMERIC:
- return cell.getNumericCellValue();
- case HSSFCell.CELL_TYPE_FORMULA:
- return cell.getNumericCellValue();
- default:
- return 0;
- }
- }
- /**
- * 取单元格内的日期,非日期的单元格返回null
- */
- public static Date getDateCellValue(HSSFCell cell) {
- if (cell == null)
- return null;
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_STRING: {
- try {
- DateFormat df = DateFormat.getDateInstance();
- return df.parse(cell.getStringCellValue());
- } catch (ParseException pe) {
- return null;
- }
- }
- case HSSFCell.CELL_TYPE_NUMERIC:
- if (HSSFDateUtil.isValidExcelDate(cell.getNumericCellValue()))
- return cell.getDateCellValue();
- case HSSFCell.CELL_TYPE_FORMULA:
- if (HSSFDateUtil.isValidExcelDate(cell.getNumericCellValue()))
- return cell.getDateCellValue();
- default:
- return null;
- }
- }
- /**
- * 取单元格内的日期,非日期的单元格返回null
- *
- * @throws ParseException
- * add by liuqi at 20110117
- */
- public static Timestamp getTimestampFromStringCellValue(HSSFCell cell)
- throws ParseException {
- if (cell.getCellType() != HSSFCell.CELL_TYPE_STRING)
- return null;
- if (cell.getStringCellValue().equals("")
- || cell.getStringCellValue() == null)
- return null;
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = sdf.parse(cell.getStringCellValue());
- if (date == null)
- return null;
- else {
- Timestamp time = new Timestamp(date.getTime());
- return time;
- }
- }
- /**
- * 取单元格内的字符串,非字符串的单元格返回""
- */
- public static String getStringCellValue(HSSFCell cell) {
- if (cell == null)
- return "";
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_BLANK:
- return "";
- case HSSFCell.CELL_TYPE_BOOLEAN:
- return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
- case HSSFCell.CELL_TYPE_STRING:
- return cell.getStringCellValue();
- case HSSFCell.CELL_TYPE_NUMERIC:
- if (HSSFDateUtil.isCellDateFormatted(cell)) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(cell.getDateCellValue());
- }
- return NumberToTextConverter.toText(cell.getNumericCellValue());
- case HSSFCell.CELL_TYPE_FORMULA:
- return cell.getStringCellValue();
- default:
- return "";
- }
- }
- public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
- String strValue) {
- HSSFRow row = sheet.getRow(x - 1);
- if (row == null)
- row = sheet.createRow(x - 1);
- HSSFCell cell = row.getCell(y - 1);
- if (cell == null)
- cell = row.createCell(y - 1);
- // if (HSSFCell.CELL_TYPE_STRING != cell.getCellType())
- // cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
- HSSFRichTextString fts = new HSSFRichTextString(strValue);
- cell.setCellValue(fts);
- return cell;
- }
- public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
- boolean blValue) {
- HSSFRow row = sheet.getRow(x - 1);
- if (row == null)
- row = sheet.createRow(x - 1);
- HSSFCell cell = row.getCell(y - 1);
- if (cell == null)
- cell = row.createCell(y - 1);
- // cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
- cell.setCellValue(blValue);
- return cell;
- }
- public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
- int nValue) {
- HSSFRow row = sheet.getRow(x - 1);
- if (row == null)
- row = sheet.createRow(x - 1);
- HSSFCell cell = row.getCell(y - 1);
- if (cell == null)
- cell = row.createCell(y - 1);
- // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
- cell.setCellValue(nValue);
- return cell;
- }
- public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
- double dValue) {
- HSSFRow row = sheet.getRow(x - 1);
- if (row == null)
- row = sheet.createRow(x - 1);
- HSSFCell cell = row.getCell(y - 1);
- if (cell == null)
- cell = row.createCell(y - 1);
- // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
- cell.setCellValue(dValue);
- return cell;
- }
- public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
- BigDecimal bValue) {
- HSSFRow row = sheet.getRow(x - 1);
- if (row == null)
- row = sheet.createRow(x - 1);
- HSSFCell cell = row.getCell(y - 1);
- if (cell == null)
- cell = row.createCell(y - 1);
- // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
- if (bValue != null)
- cell.setCellValue(bValue.doubleValue());
- else
- cell.setCellValue(0);
- return cell;
- }
- public static HSSFCell setCellFormula(HSSFSheet sheet, int x, int y,
- String strFormula) {
- HSSFRow row = sheet.getRow(x - 1);
- if (row == null)
- row = sheet.createRow(x - 1);
- HSSFCell cell = row.getCell(y - 1);
- if (cell == null)
- cell = row.createCell(y - 1);
- // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
- cell.setCellFormula(strFormula);
- return cell;
- }
- public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
- HSSFFont font) {
- if (cell == null)
- return false;
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setFont(font);
- cell.setCellStyle(cellStyle);
- return true;
- }
- public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
- HSSFFont font, String strFormat) {
- if (cell == null)
- return false;
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setFont(font);
- cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(strFormat));
- cell.setCellStyle(cellStyle);
- return true;
- }
- public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
- HSSFFont font, short sTop, short sBottom, short sLeft, short sRight) {
- if (cell == null)
- return false;
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setFont(font);
- setBordersLine(cellStyle, sTop, sBottom, sLeft, sRight);
- cell.setCellStyle(cellStyle);
- return true;
- }
- public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
- HSSFFont font, String strFormat, short sTop, short sBottom,
- short sLeft, short sRight) {
- if (cell == null)
- return false;
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setFont(font);
- cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(strFormat));
- setBordersLine(cellStyle, sTop, sBottom, sLeft, sRight);
- cell.setCellStyle(cellStyle);
- return true;
- }
- public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
- short sTop, short sBottom, short sLeft, short sRight) {
- if (cell == null)
- return false;
- HSSFCellStyle cellStyle = wb.createCellStyle();
- setBordersLine(cellStyle, sTop, sBottom, sLeft, sRight);
- cell.setCellStyle(cellStyle);
- return true;
- }
- public static void setCellFormat(HSSFCellStyle style, String strFormat) {
- if (style == null)
- return;
- style.setDataFormat(HSSFDataFormat.getBuiltinFormat(strFormat));
- }
- public static void setBordersColor(HSSFCellStyle style, short sTop,
- short sBottom, short sLeft, short sRight) {
- if (style == null)
- return;
- style.setBottomBorderColor(sBottom);
- style.setLeftBorderColor(sLeft);
- style.setRightBorderColor(sRight);
- style.setTopBorderColor(sTop);
- }
- public static void setBordersLine(HSSFCellStyle style, short sTop,
- short sBottom, short sLeft, short sRight) {
- if (style == null)
- return;
- style.setBorderBottom(sBottom);
- style.setBorderLeft(sLeft);
- style.setBorderRight(sRight);
- style.setBorderTop(sTop);
- }
- public static void setCellFont(HSSFCellStyle style, HSSFFont font) {
- if (style == null)
- return;
- style.setFont(font);
- }
- public static void setCellAlign(HSSFCell cell, short halign, short valign) {
- HSSFCellStyle cellStyle = cell.getCellStyle();
- cellStyle.setAlignment(halign);
- cellStyle.setVerticalAlignment(valign);
- cell.setCellStyle(cellStyle);
- }
- public static void setCellBkGround(HSSFCell cell, short sBkGroud) {
- HSSFCellStyle cellStyle = cell.getCellStyle();
- cellStyle.setFillForegroundColor(sBkGroud);
- cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- }
- public static void setCellBkGround(HSSFSheet sheet, int x, int y,
- short sBkGroud) {
- HSSFRow row = sheet.getRow(x - 1);
- if (row == null)
- row = sheet.createRow(x - 1);
- HSSFCell cell = row.getCell(y - 1);
- if (cell == null)
- cell = row.createCell(y - 1);
- HSSFCellStyle cellStyle = cell.getCellStyle();
- cellStyle.setFillForegroundColor(sBkGroud);
- cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- }
- public static void enableWraptext(HSSFCell cell) {
- HSSFCellStyle cellStyle = cell.getCellStyle();
- cellStyle.setWrapText(true);
- cell.setCellStyle(cellStyle);
- }
- public static void disableWraptext(HSSFCell cell) {
- HSSFCellStyle cellStyle = cell.getCellStyle();
- cellStyle.setWrapText(false);
- cell.setCellStyle(cellStyle);
- }
- public static void copyRows(HSSFSheet from, int fromRowStart,
- int fromRowEnd, HSSFSheet to, int toRowStart) {
- int startIndex = fromRowStart - 1;
- int endIndex = fromRowEnd - 1;
- int toIndex = toRowStart - 1;
- for (int index = startIndex; index <= endIndex; index++) {
- HSSFRow fromRow = from.getRow(index);
- HSSFRow fromTo = to.createRow(toIndex);
- copyRow(fromRow, fromTo);
- int num = from.getNumMergedRegions();
- for (int mergeIndex = 0; mergeIndex < num; mergeIndex++) {
- CellRangeAddress r = from.getMergedRegion(mergeIndex);
- if (index == r.getFirstRow()) {
- int add = toIndex - r.getFirstRow();
- CellRangeAddress r2 = new CellRangeAddress(r.getFirstRow()
- + add, r.getFirstColumn(), r.getLastRow() + add,
- r.getLastColumn());
- to.addMergedRegion(r2);
- }
- }
- toIndex++;
- }
- }
- public static void copyRow(HSSFRow from, HSSFRow to) {
- // set Row Height
- to.setHeight(from.getHeight());
- // set cell
- for (int i = 0; i < from.getLastCellNum() && i <= 255; i++) {
- HSSFCell cell = from.getCell(i);
- if (cell == null)
- continue;
- HSSFCell cell1 = to.createCell(i);
- cell1.setCellType(cell.getCellType());
- // cell1.setCellFormula(cell.getCellFormula());
- // cell1.setCellValue(cell.getStringCellValue());
- cell1.setCellStyle(cell.getCellStyle());
- if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
- cell1.setCellValue(cell.getNumericCellValue());
- else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
- HSSFRichTextString str = cell.getRichStringCellValue();
- if (str.length() > 0) {
- cell1.setCellValue(str);
- }
- } else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
- cell1.setCellFormula(cell.getCellFormula());
- }
- }
- public static void createimage(byte[] image, String strPicUrl) {
- try {
- FileOutputStream out = new FileOutputStream(strPicUrl);
- out.write(image);
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void deletefile(String path) {
- try {
- File f = new File(path);
- if (f.exists())
- f.delete();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void setRowHeight(HSSFSheet sheet, int nRow, float fHeight) {
- HSSFRow row = sheet.getRow(nRow - 1);
- if (row == null)
- row = sheet.createRow(nRow - 1);
- row.setHeightInPoints(fHeight);
- }
- public static void setFooter(HSSFSheet sheet, String strFontName,
- String strFooter) {
- String strSplit = " ";
- HSSFFooter footer = sheet.getFooter();
- footer.setCenter(HSSFFooter.font(strFontName, "Normal")
- + HSSFFooter.fontSize((short) 8) + strSplit + strFooter);
- }
- public static void setRegionBorders(HSSFWorkbook wb, HSSFSheet sheet,
- CellRangeAddress region) {
- if (region == null)
- return;
- for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
- HSSFRow row = sheet.getRow(i);
- if (row == null)
- row = sheet.createRow(i);
- for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
- HSSFCell cell = row.getCell(j);
- if (cell == null) {
- cell = row.createCell(j);
- }
- setCellStyle(cell, wb, HSSFCellStyle.BORDER_THIN,
- HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN,
- HSSFCellStyle.BORDER_THIN);
- }
- }
- }
- public static void main(String[] arg) {
- try {
- // FileInputStream fIn = new FileInputStream("d:\\test.xls");
- // HSSFWorkbook wb = new HSSFWorkbook(fIn);
- // HSSFSheet st = wb.getSheetAt(0);
- // HSSFCell cell = getCell(st, 4, 1);
- // System.out.println(cell.getCellFormula());
- // System.out.println(cell.getNumericCellValue());
- // HSSFRow row = st.createRow(18);
- // cell = row.createCell(8);
- // cell.setCellFormula("SUM(I1:I18)");
- // System.out.println(cell.getNumericCellValue());
- // cell.setCellValue("=SUM(I1:I18)");
- HSSFWorkbook wb = new HSSFWorkbook();
- for (int sheetNo = 1; sheetNo < 5; sheetNo++) {
- HSSFSheet st = wb.createSheet("TheSheet" + sheetNo);
- for (int rowNo = 0; rowNo < 10000; rowNo++) {
- HSSFRow row = st.createRow(rowNo);
- for (int colNo = 0; colNo < 10; colNo++) {
- HSSFCell cell = row.createCell(colNo);
- String cellValue = "Sheet:" + sheetNo + " Row:" + rowNo
- + " Col:" + colNo;
- cell.setCellValue(cellValue);
- System.out.println(cellValue);
- }
- }
- }
- FileOutputStream fOut;
- fOut = new FileOutputStream("d:\\test.xls");
- // 将模板的内容写到输出文件上
- wb.write(fOut);
- fOut.flush();
- // 操作结束,关闭文件
- fOut.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|