java操做excel

主要介紹讀取excel和寫入excel,以及更改excel中內容(模板)java

網上有不少針對03版execl寫入的教程,此處不作介紹apache

須要的jar包如圖:xss

文本中三個方法,分別對應讀取excel,修改excel,和新建excelspa

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
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.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @author 嗡嗡做響
 * 僅針對07版本excel說明,03版網上有不少教程,請自行參考
 * */
public class DoExcel {
	
	//讀取excel文件,放入Map中,實際通常是放在po中
	@SuppressWarnings("unchecked")
	public List<Map> read(String filepath) {
		XSSFWorkbook wb;
		List<Map> list = new ArrayList<Map>();
		try {
			wb = new XSSFWorkbook(new FileInputStream(filepath));
			XSSFSheet sheet = wb.getSheetAt(0);
			XSSFRow row;
			for (int i = sheet.getFirstRowNum(); i < sheet
					.getPhysicalNumberOfRows(); i++) {
				row = sheet.getRow(i);
				Map<String, String> map = new HashMap<String, String>();
				if (row.getCell(0) != null) {
					map.put("index0", row.getCell(0).toString());
				}
				if (row.getCell(1) != null) {
					map.put("index1", row.getCell(1).toString());
				}
				if (row.getCell(2) != null) {
					map.put("index2", row.getCell(2).toString());
				}
				list.add(map);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}
	//寫入新的excel中,可預設定excel模板,向模板中加數據,導出新的excel
	public void writeExcel(String filepath1) {
		Workbook wb = null;
		FileOutputStream out = null;
		try {
			wb = new XSSFWorkbook(new FileInputStream(filepath1));
			Sheet sh = wb.getSheetAt(0);
			Row row = sh.getRow(2);
			Cell cell = row.createCell(0);
			String value = "料號";
			cell.setCellValue(value);
			out = new FileOutputStream("d:\\test\\testnew.xlsx");
			//wb.write(out);
			System.out.println("OK");
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			try {
				if (out != null) {
					wb.write(out);
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
//新建excel的demo,僅做參考
	public void writeExcelDemo() {
		Workbook wb = new SXSSFWorkbook(100);//內存保留 100 條數據,避免內存溢出,其他寫入 硬盤
		Sheet sh = wb.createSheet();//建立sheet
		Row row = sh.createRow(0);//建立行
		Cell cell = row.createCell(0);//建立對應行的第幾列
		String value = "料號";
		cell.setCellValue(value);
		cell = row.createCell(1);
		value = "物料名稱";
		cell.setCellValue(value);
		FileOutputStream out;
		try {
			out = new FileOutputStream("d:\\test\\username.xlsx");//輸出文件路徑
			try {
				wb.write(out); 
				out.close();
				System.out.println("ok");
			} catch (IOException e) {
				// 
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			// 
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		//List<Map> aa = new ReadExcel().read("D:\\test\\test.xlsx");
		//System.out.println(aa);
		new DoExcel().writeExcel("D:\\test\\test111.xlsx");
		System.out.println("---");
	//	new ReadExcel().writeExcelDemo();
	}
}
相關文章
相關標籤/搜索