欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

導出Excel封裝類(POI實現)

系統 1674 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"所在位置,以此作為數據插入位置
 * 
 * @author yuxing
 * 
 */
public class ExcelTemplate {
	private static ExcelTemplate excelTemplate = new ExcelTemplate();
	private Workbook workbook;
	private int initColIndex; // 數據的初始化列數
	private int initRowIndex; // 數據的初始化行數
	private int curColIndex; // 數據當前列數
	private int curRowIndex; // 數據當前行數
	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("寫入數據失敗:"+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最大行數65536!

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

??

導出Excel封裝類(POI實現)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 美女网站在线观看视频18 | 免费xxxx日本大片在线观看 | 国产日韩久久久精品影院首页 | 欧美人两个人激情的免费视频 | 日本黄色大片免费看 | 啪啪天堂 | 特黄一区二区三区 | 久久www免费人成看片高清 | 色视频一区 | 国产亚洲精品久久久久婷婷图片 | 国产精品第三页在线看 | 特黄免费| 欧美国产免费 | av国语| 一道本在线观看视频 | 日日操日日干 | 午夜影院普通 | 精品久久久久久久久久久久 | 亚洲精品人成网在线播放蜜芽 | 亚洲欧美视频一区 | 欧美精品一区二区三区免费播放 | 免费观看日本a毛片 | 国产亚洲欧美日本一二三本道 | 成人自拍偷拍视频 | 六月激情婷婷 | 国产一区二区三区高清 | 成人福利视频在线看高清观看 | 色涩亚洲 | 亚洲成人精品久久久 | 三级免费网| 国产一区二区三区免费观看 | 欧美二区三区 | 亚洲第一男人天堂 | 成年美女黄的视频网站 | 久久不卡一区二区三区 | 在线天堂中文在线资源网 | 国产精品一区二区三区在线播放 | a级片免费观看视频 | 不卡中文一二三区 | 波多野结衣全部系列在线观看 | 欧美区一区二区三 |