欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 国产精品成人一区二区 | 国产亚洲精彩视频 | 成人免费电影av | 天天看逼 | 成年做羞羞免费观看视频网站 | 免费一级片在线观看 | 久久久入口 | 小蝌蚪污视频 | 日韩一区二区在线视频 | 日韩电影网站 | 超碰成人免费 | 第一页在线视频 | 亚洲一区二区三区免费视频 | 亚洲精品一区二区三区福利 | 亚洲国产精品网站 | 亚洲一在线 | 国产亚洲精品久久久久久老妇 | 精品av | www.av88| 精品亚洲永久免费精品 | 奇米影视亚洲精品一区 | 国产99精品 | 亚洲九九 | 91久久精品一区二区二区 | 在线观看国产日韩欧美 | 女人午夜色又刺激黄的视频免费 | 一区二区三区视频免费 | 成人在线视频网站 | 9l蝌蚪porny中文自拍 | av影音资源 | 9久久9久久精品 | 久久av网 | 浮力影院网站午夜 | 一级毛片视频播放 | 激情五月婷婷综合网 | 欧美午夜a级限制福利片 | 97视频免费播放观看在线视频 | 欧美成人精品一区二区三区 | 色AV亚洲AV永久无码精品软件 | 国产亚洲精品久久久久久国 | 中文久久 |