poi 操作excel

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MrwanZH/article/details/83411318

1、removeRow(Row row):删除行,但不会实现下面行上移

2、shiftRows(int startRow, int endRow, n):start行到end行移动n行,n正上移,n负下移。合并过单元格的移动会报错

3、removeMergedRegion(int deleRow):删除样式

4、表、行、单元格循环的时候都是从0开始读

5、shiftRows无法向上移动,个人猜测,removeRow删除行只有在write方法调用后才生效,所以上移会出现合并的单元格无法移动。

public void func(Workbook wb) {
	for (int i = 0; i < wb.getNumberOfSheets(); i++) {
		Sheet sheet = wb.getSheetAt(i);
		for (int rr = 0; rr < sheet.getPhysicalNumberOfRows(); rr++) { // 遍历有记录的所有行,空行不记录
			Row row = sheet.getRow(rr);
			if (row == null) {
				continue;
			}
			for (int cc = 0; cc < row.getPhysicalNumberOfCells(); cc++) { // 遍历所有列,空列不记录
				Cell cell = row.getCell(cc);
				CellType cellType = cell.getCellTypeEnum();
				String value;
				switch (cellType) {
				case STRING:
					value = cell.getStringCellValue();
				case NUMERIC:
					value = String.valueOf(cell.getNumericCellValue());
				case BOOLEAN:
					value = String.valueOf(cell.getBooleanCellValue());
				default:
					value = "";
				}
				cell.setCellValue(value);
			}
		}
	}
}

二、关于POI3.17的一些问题

1、使用poi3.17版本生成excel的一些样式设置http://blog.sina.com.cn/s/blog_c2aff7060102xlj4.html

2、不推荐getCellType(),推荐getCellTypeEnum()

3、POI使用详解https://www.cnblogs.com/huajiezh/p/5467821.html

4、Workbook wb = new HSSFWorkbook();  //.xls  excel95, 97, 2000,2003等版本

                            = new XSSFWorkbook(); //.xlsx  excel2007及以后版本

5、Sheet sheet = wb.createSheet("name");//sheet

6、Row row = sheet.createRow(0);            //创建row   某行第0行

row.setHeightInPoints(50);                //设置行高

7、Cell cell = row.createCell(0);        //创建cell

如果要多列并合并单元格 并且给多列设置相同的样式,则要创建多个cell,每个cell都要调用setCellStyle(style)

CellRangeAddress region = new CellRangeAddress(0, 1, 0, 3);        //合并列     0-1行  0-3列 合并单元格
sheet.addMergedRegion(region);                                //设置合并格式

8、cellStyle

CellStyle style = wb.createCellStyle();//单元格格式
        style.setBorderTop(BorderStyle.THIN);        //上边框
        style.setBorderBottom(BorderStyle.THIN);    //下边框
        style.setBorderLeft(BorderStyle.THIN);        //左边框
        style.setBorderRight(BorderStyle.THIN);        //右边框
        style.setAlignment(HorizontalAlignment.CENTER);            //水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);    //垂直居中

9、Font

    public Font createCellFont(Workbook wb){
        Font font = wb.createFont();            //设置字体类
        font.setFontHeightInPoints((short) 10);    //设置字号
        font.setFontName("ARIAL");                    //设置字体
        font.setBold(false);                        //设置是否加粗
        font.setColor(IndexedColors.BLACK.index);    //设置字体颜色

猜你喜欢

转载自blog.csdn.net/MrwanZH/article/details/83411318