java xls 操作(一)

来源:互联网 发布:移动医护软件 编辑:程序博客网 时间:2024/04/27 20:30

所需的jar包为poi的jar包

1、 根据单元格值类型获取对应的值

private String getCellValue(HSSFCell cell) {    if (cell == null) {            return "";    }   String cellValue = "";   Switch(cell.getCellType()) {        case HSSFCell.CELL_TYPE_BLANK:            break;        case HSSFCell.CELL_TYPE_BOOLEAN:            cellValue = (cell.getBooleanCellValue() + "").trim();            break;        case HSSFCell.CELL_TYPE_NUMERIC:            //如果是时间格式                if (HSSFDateUtil.isCellDateFormatted(cell) ){                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");                    cellValue = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();                    break;       }    else {            cellValue = new DecimalFormat("0").format(cell.getNumericCellValue()).trim();            break;    }    case HSSFCell.CELL_TYPE_STRING:        cellValue = cell.getStringCellValue().trim();        break;    case HSSFCell.CELL_TYPE_FORMULA: //取出来的是公式        try {            cellValue = cell.getCellFormula() + "";        } catch( Exception e) { cellValue = cell.getErrorCellString();}         break;    case HSSFCell.CELL_TYPE_ERROR:        cellValue = "非法字段"break;    default:         cellValue = "未知类型";        break;        }   return cellValue;}

2、 设置行高和列宽

//行高HSSFRow row = sheet.creatRow(0); //获取某一行row.setHeightInPoints(24); // 代表行高24row.setHeith(24); //代表多少没有测试过//列宽sheet.setColumnWidt(0,10*256); //0代表第一列,10* 256代表列的宽度  以一个字符的1/256的宽度作为一个单位   3000的话就是11.7左右 舍去小数就是11个字符的宽度  转像素的话先要看字符的类型,不同的类型一个字符占用的像素是不同的

3、 设置单元格颜色样式、字体、边框、居中和自动换行
注: 每个单元格一个style对象,不可以重复

a: 设置背景色HSSFCellStyle style = workbook.createCellStyle();style.setFillForegroundColor((shot)13); //设置背景色  或者HSSFCellStyle.GREEN.indexstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //填充模式,前景色注:这个是采用poi内定的样式,也可以自己定义样式a1: 采用自定义样式HSSFPalette palette = workbook.getCustomPalette();palette.setColorAtIndex((short)9, (byte)(color.getRed()), (byte)(color.getGreen()), (byte)(color.getBlue()));//设置颜色的索引即上例中的9,只能在8~64之间,在此之外的索引无效,也不会报错。palette.setColorAtIndex((short)10, (byte)(0xff & 251), (byte)(0xff & 161), (byte)(0xff & 161));//中间最好是转为16进制数//使用HSSFStyle style = workbook.createCellStyle();style.setFillForegroundColor((short)9);b: 设置边框style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //左边框style.setBorderTop(HSSFCellStyle.BORDER_THIN); //上边框style.setBorderRight(HSSFCellStyle.BORDER_THIN); //右边框c: 居中style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中style.setVerticalAlignment(HSSFCellStyle.Vertical_Align_center);//垂直居中d: 字体HSSFFont font = workbook.createFont();font.setFontName("黑体");//字体名称font.setFontHeightInPoints((short)16);//设置字体大小font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示style.setFont(font);//选择需要用到的字体格式e: 设置自动换行style.setWrapText(true);//设置自动换行

4、 格式化单元格日期信息

HSSFDataFormat format = workbook.createDataFormat();short format = format.getFormat("yyyy-mm-dd HH:MM");HSSFCellStyle style = workbook.createCellStyle();style.setDataFarmat(format);cell.setCellValue(new Date());cell.setCellStyle(style);

5、 合并单元格

a: 合并单元格Region region = new Region(0, (short)0, 0, (short)6);//参数1->起始行号  参数2->起始列号 参数3->终止行号 参数4->终止列号或者CellRangeAddress region = new CellRangeAddress(rowNumber, rowNumber, (short)0, (short)1);两个方法的参数是不一样的,具体使用哪个取决于poi的不同版本sheet.addMergedRegion(region);b: 获取合并单元格的值//合并单元格只会在第一个单元格有值,获取第一个合并单元格即可int sheetMergeCount = sheet.getNumMergedRegions();for (int i = 0; i < sheetMergeCount; i++){    CellRangeAddress cs = sheet.getMergedRegion(i);    int firstColumn = ca.getFirstColumn();    int lastColumn = ca.getLastColumn();    int firstRow = ca.getFirstRow();    int lastRow = ca.getLastRow();    int currentCellRowIndex = cell.getRowIndex();    int currentCellColumnIndex = cell.getColumnIndex();    if (currentCellRowIndex >= firstRow && currentCellRowIndex <= lastRow && currentCellColumnIndex >= firstColumn && currentCellColumnIndex <= lastColumn){        //表明单元格是合并单元格        String value = getcellValue(firstRow, firstColumn);    }}

7、获取表格中的所有sheeet页、行数、列数

for(int i= 0; i < workbook.getNumberOfSheets(); i++) {    int rowNum = sheet.getPhysicalNumerOfRows();    int rowNum2 = sheet.getLastRowNum()    // rowNum获取的是物理行数,也就是不包括那些空行(隔行)的情况,即遇到空行,不计算在内,此时一般调用getNextPhysicalRow();rowNum2获取的是最后一行的编号(编号从0开始),即获取的是所有行,但是会遇到空行,需要做判空处理,for (int i=0; i<=rowNum2;i++);    int cellNum = row.getPhysicalNumberOfCells();    int cellNum2 = row.getLastCellNum();}

8、当需要生成一个电子表格时,可以采用两种方法

//a:生成一个空的// b. 搞一个表格模块,直接生成XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(File));workbook.setSheetName(1, "111"); // 修改名称后,需要生成文件才能生效// 处理....File xlsxFile = new File(path); // 最终要生成的文件FileOutputStrem outputStream = null;try {    outputStream = new FileOutputStream(xlsxFile);    workbook.writer(outputStream);    outputStream.flush();} catch (IOException e ) {    e.printStackTrace();} finally {    if ( outputStream != null) {        try {outputStream.close()}        catch (Exception e ) {e.printStackTrack();}    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 5个月宝宝不爱吃奶怎么办 九个月宝宝一直流鼻涕怎么办 九个月宝宝一直咳嗽怎么办 宝宝3岁不爱喝水怎么办 1岁宝宝不肯喝水怎么办 三个月宝宝体检说严重缺钙怎么办 1岁半宝宝不吃药怎么办 1岁宝宝抗拒吃药怎么办 六个月宝宝不爱吃辅食怎么办 宝宝九个月了不爱吃辅食怎么办 八个月宝宝不喜欢吃辅食怎么办 小孩米粉吃多了怎么办 宝宝四个月了奶水不足怎么办 4个月奶水不足怎么办 孩子不吃奶粉母乳又不够怎么办 宝宝吃母乳上火了怎么办 5个月宝宝厌奶期怎么办 九个月宝宝不吃奶粉怎么办 第5个月奶不够吃怎么办 九个月的宝宝不吃奶粉怎么办 9个月宝宝不肯吃怎么办 11个月不吃辅食怎么办 4个月母乳不足怎么办 宝宝四个月奶不够怎么办 四个月宝宝奶不够吃怎么办 宝宝吃母乳偏瘦怎么办 宝宝吃母乳很瘦怎么办 8个月宝宝流汗太多怎么办 奶水多乳房胀疼怎么办 乳房胀奶奶水减少怎么办 宝宝五个月奶水不够吃怎么办 梦见鬼在梦里怎么办 宝宝晚上奶水不够吃怎么办 十个月晚上奶水不够吃怎么办 产妇晚上奶水不够吃怎么办 刚出生的宝宝不吃母乳怎么办 宝宝六个月奶不够吃怎么办 六个月奶不够吃怎么办 刚出生奶不够吃怎么办 做梦醒了看见鬼怎么办 宝宝到陌生地方哭闹怎么办