java导入导出excel相关

<a target=_blank target="_blank" href="http://my.oschina.net/u/927151/blog/349284" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">点击查看原网页解析excel导出</a>

第二篇相关文章


hssf,xssf和sxssf的区别

HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现 
XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现 
可以理解为hssf是对xsl文件的操作,xssf是对xlsx文件的操作
以下摘自:http://wenku.baidu.com/view/b9023573b307e87101f6965f.html 
package com.cvt.smarthome.application.basedata.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.RichTextString;

/**
 * 共分为六部完成根据模板导出excel操作:<br/>
 * 第一步、设置excel模板路径(setSrcPath)<br/>
 * 第二步、设置要生成excel文件路径(setDesPath)<br/>
 * 第三步、设置模板中哪个Sheet列(setSheetName)<br/>
 * 第四步、获取所读取excel模板的对象(getSheet)<br/>
 * 第五步、设置数据(分为种类型数据:setCellStrValue、setCellDateValue、setCellDoubleValue、setCellBoolValue、setCellCalendarValue、
 * setCellRichTextStrValue)<br/>
 * 第六步、完成导出 (exportToNewFile)<br/>
 * 
 * @author Administrator
 * 
 */
public class ExcelUtil {
	private String	srcXlsPath	= "";	// // excel模板路径
	private String	desXlsPath	= "";
	private String	sheetName	= "";
	POIFSFileSystem	fs			= null;
	HSSFWorkbook	wb			= null;
	HSSFSheet		sheet		= null;

	/**
	 * 第一步、设置excel模板路径
	 * 
	 * @param srcXlsPath
	 */
	public void setSrcPath(String srcXlsPath) {
		this.srcXlsPath = srcXlsPath;
	}

	/**
	 * 第二步、设置要生成excel文件路径
	 * 
	 * @param desXlsPath
	 */
	public void setDesPath(String desXlsPath) {
		this.desXlsPath = desXlsPath;
	}

	/**
	 * 第三步、设置模板中哪个Sheet列
	 * 
	 * @param sheetName
	 */
	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	/**
	 * 第四步、获取所读取excel模板的对象
	 */
	public void getSheet() {
		try {
			File fi = new File(srcXlsPath);
			if (!fi.exists()) {
				System.out.println("模板文件:" + srcXlsPath + "不存在!");
				return;
			}
			fs = new POIFSFileSystem(new FileInputStream(fi));
			wb = new HSSFWorkbook(fs);
			sheet = wb.getSheet(sheetName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 第五步、设置字符串类型的数据
	 * 
	 * @param rowIndex
	 *            --行值
	 * @param cellnum
	 *            --列值
	 * @param value
	 *            --字符串类型的数据
	 */
	public void setCellStrValue(int rowIndex, int cellnum, String value) {
		HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
		cell.setCellValue(value);
	}

	/**
	 * 第五步、设置日期/时间类型的数据
	 * 
	 * @param rowIndex
	 *            --行值
	 * @param cellnum
	 *            --列值
	 * @param value
	 *            --日期/时间类型的数据
	 */
	public void setCellDateValue(int rowIndex, int cellnum, Date value) {
		HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
		cell.setCellValue(value);
	}

	/**
	 * 第五步、设置浮点类型的数据
	 * 
	 * @param rowIndex
	 *            --行值
	 * @param cellnum
	 *            --列值
	 * @param value
	 *            --浮点类型的数据
	 */
	public void setCellDoubleValue(int rowIndex, int cellnum, double value) {
		HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
		cell.setCellValue(value);
	}

	/**
	 * 第五步、设置Bool类型的数据
	 * 
	 * @param rowIndex
	 *            --行值
	 * @param cellnum
	 *            --列值
	 * @param value
	 *            --Bool类型的数据
	 */
	public void setCellBoolValue(int rowIndex, int cellnum, boolean value) {
		HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
		cell.setCellValue(value);
	}

	/**
	 * 第五步、设置日历类型的数据
	 * 
	 * @param rowIndex
	 *            --行值
	 * @param cellnum
	 *            --列值
	 * @param value
	 *            --日历类型的数据
	 */
	public void setCellCalendarValue(int rowIndex, int cellnum, Calendar value) {
		HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
		cell.setCellValue(value);
	}

	/**
	 * 第五步、设置富文本字符串类型的数据。可以为同一个单元格内的字符串的不同部分设置不同的字体、颜色、下划线
	 * 
	 * @param rowIndex
	 *            --行值
	 * @param cellnum
	 *            --列值
	 * @param value
	 *            --富文本字符串类型的数据
	 */
	public void setCellRichTextStrValue(int rowIndex, int cellnum,
		RichTextString value) {
		HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
		cell.setCellValue(value);
	}

	/**
	 * 第六步、完成导出
	 */
	public void exportToNewFile() {
		FileOutputStream out;
		try {
			out = new FileOutputStream(desXlsPath);
			wb.write(out);
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

以下为自己仿写的用于xslx格式的导出excel(根据模板)
public class poiutil {
		public String inModelPath;		//模板路径
		public String outXslxPath;		//输出路径
		public String caselsh;
		
		FileInputStream fis;
		XSSFSheet sheet;				//需要用到的sheet		
		XSSFWorkbook workBook;			
		
        public String getCaselsh() {
			return caselsh;
		}
		public void setCaselsh(String caselsh) {
			this.caselsh = caselsh;
		}
		public String getInModelPath() {
			return inModelPath;
		}
		public void setInModelPath(String inModelPath) {
			this.inModelPath = inModelPath;
		}
		public String getOutXslxPath() {
			return outXslxPath;
		}
		public void setOutXslxPath(String outXslxPath) {
			this.outXslxPath = outXslxPath;
		}
		//获取sheet
		public void getSheet(){
			try {
				 fis = new FileInputStream(inModelPath);
				 workBook=new XSSFWorkbook(fis);
				 sheet= workBook.getSheetAt(0);          //获取模板
			} catch (Exception e) {
				e.printStackTrace();
			}  
		}
		//为sheet需要改变的部分赋值
		public void setCellStrValue(int rowIndex, int cellnum, String value) {  
			 
			XSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);  
		    cell.setCellValue(value);  
		} 
		//输出excel
		public void exportExcel(){
			String fileName=caselsh+".xslx";
            try {
				OutputStream out=new FileOutputStream(outXslxPath+fileName);
				 workBook.write(out);      
                 fis.close();
                 out.flush();  
                 out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
}

---------------------------文件上传到服务器---------------------------------

request.getSession().getServletContext().getRealPath(logoPathDir/保存的目录)

这句话表示文件上传服务器的目录。






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值