欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POI的一些操作 获取、删除区域Region 删除多行

程序员文章站 2022-06-13 08:16:37
...

1 通过行列获取区域

 

	/**
	 * 获取区域 Region
	 * @param sheet
	 * @param row
	 * @param column
	 * @return
	 */
	public static int getMergedRegionIndex(Sheet sheet, int row, int column) { 
		int sheetMergeCount = sheet.getNumMergedRegions(); 

		for (int i = 0; i < sheetMergeCount; i++) { 
			CellRangeAddress ca = sheet.getMergedRegion(i); 
			int firstColumn = ca.getFirstColumn(); 
			int lastColumn = ca.getLastColumn(); 
			int firstRow = ca.getFirstRow(); 
			int lastRow = ca.getLastRow(); 
	
			if (row >= firstRow && row <= lastRow) { 
				if (column >= firstColumn && column <= lastColumn) { 
					return i; 
				} 
			} 
		} 

		return 0;
	}

 

 

2 删除区域

 

sheet.removeMergedRegion(getMergedRegionIndex(sheet, 1, 1));

 

 

3 清除行

 

sheet.removeRow(sheet.getRow(0));

 

 

4 删除行(注意:删除行前,须删除区域Region)

 

 

// 第1个参数  开始行
// 第2个参数  结束行
// 第3个参数  覆盖行数
// 第4个参数  是否带原格式copyRowHeight 
// 第5个参数  是否重设区域resetOriginalRowHeight 
sheet.shiftRows(8, 8, -3, true, true);

 

try {
        	InputStream is = null;
            Workbook book = null;
            is = new FileInputStream("D:\\test.xls");
            book = new HSSFWorkbook(is);
            
            Sheet sheet = book.getSheetAt(0);
            sheet.shiftRows(1, 1, -1);
            
            File f = new File("d:\\1.xls");
            FileOutputStream out = new FileOutputStream(f);
            book.write(out);
            
            out.close();
            is.close();
        } catch (IOException e) {
            return;
        }

 

 

5 获取操作系统

	public static void getProperties() {
		Properties prop = System.getProperties();
		String os = prop.getProperty("os.name");
		System.out.println(os);
	}

 

 6 classLoader顺序

 

	public static void getClassLoader() {
		System.out.println(ClassLoader.getSystemClassLoader());
		System.out.println(ClassLoader.getSystemClassLoader().getParent());
		System.out.println(ClassLoader.getSystemClassLoader().getParent().getParent());
	}

 

7 汉字排序

 

public static void sortMap() {
		List list = new ArrayList();
		ProductPos p = new ProductPos();
		p.setPosId("2");
		list.add(p);
		
		p = new ProductPos();
		p.setPosId("张三");
		list.add(p);
		
		p = new ProductPos();
		p.setPosId("王武");
		list.add(p);
		
		System.out.println(list);
		
		Collections.sort(list, new Comparator<ProductPos>() {
			 public int compare(ProductPos arg0, ProductPos arg1) {
	                return arg0.getPosId().compareTo(arg1.getPosId());
	            }
		});
		System.out.println(list);
		
		//Collections.sort(list, (RuleBasedCollator )Collator.getInstance(Locale.CHINA));
		
		Collections.sort(list, new Comparator<Date>() {
			 public int compare(Date arg0, Date arg1) {
				 return Collator.getInstance(Locale.CHINA).compare(arg0.getTime(), arg0.getTime());
	            }
		});
		System.out.println(list);
		
	}

 

8 文件拷贝

public static void fileUpload(File file, String filename) {
		try {
			file.renameTo(new File(filename));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

 

 

 

 

相关标签: java poi