POI操作

合併單元格,在HSSF中合併單元格是如下的操作

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
 
    HSSFRow row = sheet.createRow((short) 1);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of merging");
 
    sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));//指定合併區域
 
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();



讀excel判斷   不存在集合

 <pre name="code" class="java">package com.test;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class EventExample {
private static	Workbook xls = null;
	public static Map<String,String> read(String filePath, int cell,int start,int end) throws IOException {
		Map<String,String> map=new HashMap<>();
		
		FileInputStream fileInputStream = new FileInputStream(new File(filePath));

		if (filePath.endsWith("xlsx")) {

			// 2007

			xls = new XSSFWorkbook(fileInputStream);

		} else if (filePath.endsWith("xls")) {

			// 2003

			xls = new HSSFWorkbook(fileInputStream);

		}
		
		  Sheet sheet = xls.getSheetAt(0);
		  
		  
		for(int i=start;i<(end+1);i++){
		 String result=sheet.getRow(i).getCell(cell).getStringCellValue();
		 
		map.put(result,result);
		 
		}
 
	
	return map; 

	}

	public static void main(String args[]) throws Exception {
		
	/*	
	 * 知識點
	 * Map<String, String> map=read("E:/java/工作/標準/知識點標準結構-高中.xls",1,2,56);
	
	 Map<String, String> map2=read("E:/java/工作/標準/田中雙向細目表.xls",3,2,362);
	*/ 
		
		Map<String, String> map=read("E:/java/工作/標準/能力值標準結構-高中.xls",1,2,39);
		
		 Map<String, String> map2=read("E:/java/工作/標準/田中雙向細目表.xls",4,2,362);
		
	 List<String> list=new ArrayList<>();
	 list.addAll(map2.values());
	 
	 
	  
	 int i=0;
	 for(String s:list){
		 
		
		if(null==map.get(s) ){
	  System.out.println( s);
			i++;
		}
	  }
	 System.out.println(i);
	
 
		 
}
	

	
	public static void setCol(String filePath,int rows) throws IOException{
	CellStyle s=xls.createCellStyle();
     	s.setFillBackgroundColor(HSSFColor.BLUE.index);
              Map<String,String> map=new HashMap<>();
		
		FileInputStream fileInputStream = new FileInputStream(new File(filePath));

		if (filePath.endsWith("xlsx")) {

			// 2007

			xls = new XSSFWorkbook(fileInputStream);

		} else if (filePath.endsWith("xls")) {

			// 2003

			xls = new HSSFWorkbook(fileInputStream);

		}
		
		  Sheet sheet = xls.getSheetAt(0);
		  
		  sheet.getRow(rows).setRowStyle(s);
	
	}

}


注意

HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,擴展名是.xls 
XSSFWorkbook:是操作Excel2007的版本,擴展名是.xlsx
發佈了35 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章