亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

導出Excel封裝類(POI實現(xiàn))

系統(tǒng) 1818 0
    
      ?package com.yuxinglab.poi.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ss.usermodel.WorkbookFactory;

/**
 * Excel模版類 提供Excel模版文件,找到"datas"所在位置,以此作為數(shù)據(jù)插入位置
 * 
 * @author yuxing
 * 
 */
public class ExcelTemplate {
	private static ExcelTemplate excelTemplate = new ExcelTemplate();
	private Workbook workbook;
	private int initColIndex; // 數(shù)據(jù)的初始化列數(shù)
	private int initRowIndex; // 數(shù)據(jù)的初始化行數(shù)
	private int curColIndex; // 數(shù)據(jù)當前列數(shù)
	private int curRowIndex; // 數(shù)據(jù)當前行數(shù)
	private Row curRow; // 當前行
	private Sheet sheet;

	private ExcelTemplate() {
	}

	public static ExcelTemplate getInstance() {
		return excelTemplate;
	}

	public final static String DATA_BEGIN = "datas";

	// 讀取模版
	public ExcelTemplate readExcelTemplateFromClassPath(String path) {
		try {
			workbook = WorkbookFactory.create(ExcelTemplate.class
					.getResourceAsStream(path));
			initTemplate();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
			throw new RuntimeException("讀取模版文件失敗!請檢查文件格式.");
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("模版文件不存在!請檢查.");
		}
		return this;
	}

	public ExcelTemplate readExcelTemplateFromPath(String path) {
		try {
			workbook = WorkbookFactory.create(new File(path));
			initTemplate();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
			throw new RuntimeException("讀取模版文件失敗!請檢查文件格式.");
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("模版文件不存在!請檢查.");
		}
		return this;
	}

	private void initTemplate() {
		sheet = workbook.getSheetAt(0);
		initConfigData();
		createRow();

	}

	private void initConfigData() {
		boolean flag = false;
		for (Row row : sheet) {
			if (flag) {
				break;
			}
			for (Cell c : row) {
				if (c.getCellType() != c.CELL_TYPE_STRING) {
					continue;
				}
				String str = c.getStringCellValue().trim();
				if (str.equals(DATA_BEGIN)) {
					initColIndex = c.getColumnIndex();
					initRowIndex = row.getRowNum();
					curColIndex = initColIndex;
					curRowIndex = initRowIndex;
					flag = true;
					break;
				}

			}
		}
	}

	public static void main(String[] args) {
		ExcelTemplate excelTemplate = getInstance().readExcelTemplateFromPath(
				"d:/template.xls");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createCell("111");
		excelTemplate.createRow();
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createCell("222");
		excelTemplate.createRow();
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createCell("333");
		excelTemplate.createRow();
		excelTemplate.writeToFile("d:/01.xls");
	}

	public void createCell(String value) {
		curRow.createCell(curColIndex).setCellValue(value);
		curColIndex++;
	}

	public void createRow() {
		curRow = sheet.createRow(curRowIndex);
		curRowIndex++;
		curColIndex = initColIndex;

	}

	public void writeToFile(String filePath) {
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(filePath);
			workbook.write(fileOutputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("寫入的文件" + filePath + "不存在!");
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("寫入數(shù)據(jù)失敗:"+e.getMessage());
		} finally {
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void writeToStream(OutputStream outputStream) {
		try {
			workbook.write(outputStream);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("寫入流失敗:"+e.getMessage());
		}
	}
}

    

?對于web應用,可以使用如下代碼:

      response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition",
				"attachment;filename=file.xls");
		OutputStream ouputStream = null;
		try {
			ouputStream = response.getOutputStream();
			excelTemplate.writeToStream(ouputStream);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				ouputStream.flush();
				ouputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    

?效果如下:

? Excel最大行數(shù)65536!

?注意jar包是否沖突! 導出Excel封裝類(POI實現(xiàn))

??

導出Excel封裝類(POI實現(xiàn))


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲国产精品免费在线观看 | 国产美女久久 | 99久久99久久精品免费看子 | 国产精品福利久久2020 | 国产91一区二这在线播放 | 青青青免费在线视频 | 国产一级特黄a大片99 | 国产天堂| 亚洲狠狠 | 亚洲啪啪免费视频 | 97久久国产一区二区三区四区 | 日本人成免费大片 | 亚洲日本在线播放 | 91激情视频 | 日韩一区二区超清视频 | www.国产一区二区三区 | 久久综合爱 | 亚洲一区二区三区精品视频 | 高清性色生活片久久久 | 欧美色另类 | 91午夜精品亚洲一区二区三区 | 亚洲人成依人成综合网 | 正在播放亚洲一区 | 五月婷婷在线观看视频 | 成人看片黄a免费看视频 | 狼人射综合 | 亚洲成人福利 | 国产你懂的 | 四虎免费影院4hu永久免费 | 99在线视频播放 | 99热久久免费精品首页 | 久久国产精品99国产精 | 日韩一级一片 | 日日摸日日碰夜夜爽久久 | 久久七国产精品 | 国产原创麻豆精品视频 | 九九热视频免费在线观看 | 一区二区三区免费精品视频 | 日韩在线操 | 久青草国产手机视频免费观看 | 四虎国产成人永久精品免费 |