excel解析工具类

<span style="font-family:Microsoft YaHei;font-size:14px;">public class FileExcelOperation {

	private WritableWorkbook fWorkBook = null;
	private WritableWorkbook newReadBook = null;
	private Workbook fReadBook = null;
	
	/**
	 * 创建读取EXCEL
	 * @param fileName
	 * @return
	 */
	public Workbook readExcel(String fileName)
	{
		File f = new File(fileName);
		if (!f.exists())
		{
			throw new SystemException("文件不存在");
		}
		try {
			fReadBook = Workbook.getWorkbook(f);
		} catch (Exception e) {
			throw new SystemException(e.getMessage());
		}
		return fReadBook;
	}
	/**
	 * 获取数据页通过索引
	 * TODO
	 * 
	 * @param index
	 * @return Sheet
	 * 
	 * history
	 *
	 */
	public Sheet readSheet(int index)
	{
		if(fReadBook == null)
		{
			throw new SystemException("读取EXCEL工作薄不能为空");
		}
		return fReadBook.getSheet(index);
	}
	/**
	 * 获取数据页通过页签
	 * TODO
	 * 
	 * @param name
	 * @return Sheet
	 * 
	 * history
	 *
	 */
	public Sheet readSheet(String name)
	{
		if(fReadBook == null)
		{
			throw new SystemException("读取EXCEL工作薄不能为空");
		}
		return fReadBook.getSheet(name);
	}
	public Cell getCell(Sheet activeSheet , int col , int row)
	{
		return activeSheet.getCell(col , row);
	}
	
	public String getCellValue(Sheet activeSheet , int col , int row)
	{
		return activeSheet.getCell(col , row).getContents().toString();
	}
	public int getRowCount(Sheet activeSheet)
	{
		return activeSheet.getRows();
	}
	public int getColumnCount(Sheet activeSheet)
	{
		return activeSheet.getColumns();
	}
	/**
	 * 创建写入EXCEL
	 * @param fileName
	 * @return
	 */
	public WritableWorkbook openExcel(String fileName)
	{
		if (fWorkBook == null)
		{
			String str = fileName.substring(0 , fileName.lastIndexOf(File.separator));
			 File dir = new File(str+File.separator);
			 if (!dir.exists())
			 {
				 dir.mkdirs();
			 }
		}
		File f = new File(fileName);
		try {
			fWorkBook = Workbook.createWorkbook(f);
		} catch (IOException e) {
			throw new SystemException(e.getMessage());
		}
		return fWorkBook;
	}
	
	public WritableWorkbook copeWritableWorkbook(String fileName){
		String str = fileName.substring(0 , fileName.lastIndexOf(File.separator));
		File dir = new File(str+File.separator);
		if (!dir.exists())
		{
			dir.mkdirs();
		}
		try {
			newReadBook = Workbook.createWorkbook(new File(fileName), fReadBook);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newReadBook;
	}
	
	public WritableSheet getNewSheet(int index)
	{
		return newReadBook.getSheet(index);
	}
	
	public WritableSheet getSheet(int index)
	{
		return fWorkBook.getSheet(index);
	}
	public WritableSheet getSheet(String name)
	{
		return fWorkBook.getSheet(name);
	}
	public WritableSheet createSheet(String name)
	{
		return fWorkBook.createSheet(name, 0);
	}
	public WritableSheet createSheet(String name , int index)
	{
		return fWorkBook.createSheet(name, index);
	}
	
	public Cell getCell(WritableSheet activeSheet , int col , int row)
	{
		return activeSheet.getCell(col , row);
	}
	
	public String getCellValue(WritableSheet activeSheet , int col , int row)
	{
		return activeSheet.getCell(col , row).getContents().toString();
	}
	public void setCellValue(WritableSheet activeSheet , int col , int row , String value)
	{
		try {
			activeSheet.addCell(new Label(col, row, value));
		} catch (Exception e) {
			throw new SystemException(e.getMessage());
		} 
	}
	
	public int getRowCount(WritableSheet activeSheet)
	{
		return activeSheet.getRows();
	}
	public int getColumnCount(WritableSheet activeSheet)
	{
		return activeSheet.getColumns();
	}
	/**
	 * 
	 * @param activeSheet 激活页
	 * @param l 数据LIST
	 * @param cap 头LIST
	 * @param sort 排序LIST
	 * @param startRow 起始行
	 * @param startCol 起始列
	 */
	
	@SuppressWarnings("unused")
	public void setExcelValue(WritableSheet activeSheet , List l , List cap , List sort , int startRow , int startCol)
	{
		if (l.size() <=0)
		{
			return;
		}
		int row = startRow ;
		if (cap != null && cap.size()>0)
		{
			for (int i = 0 ; i<cap.size() ; i++)
			{
				setCellValue(activeSheet , startCol +i , startRow , cap.get(i).toString());
			}
			
		} else {
			if (sort != null && sort.size()>0)
			{	
				for (int r = 0 ; r < sort.size() ;r++)
				{
					Map m = (Map)l.get(0);
					Iterator it = m.entrySet().iterator();
					int i =0;
					while(it.hasNext())
					{
						Map.Entry t = (Map.Entry) it.next();
						if (sort.get(r).toString().equals(t.getKey().toString()))
						{
							setCellValue(activeSheet , startCol +r , startRow , t.getKey().toString());	
							i++;
							break;
						}	
						i++;
						
					}
				}
			} else {
				Map m = (Map)l.get(0);
				Iterator it = m.entrySet().iterator();
				int i =0;
				while(it.hasNext())
				{
					Map.Entry t = (Map.Entry) it.next();
					setCellValue(activeSheet , startCol +i , startRow , t.getKey().toString());	
					i++;
				}
			}
		}
		row += 1;
		
		if (sort != null && sort.size()>0)
		{
			for (int i = 0 ; i<l.size() ; i++)
			{
				Map m = (Map)l.get(i);
				for (int c = 0 ; c<sort.size() ; c++)
				{
					if (m.containsKey(sort.get(c).toString()))
					{
						setCellValue(activeSheet , startCol +c , row +i  , m.get(sort.get(c).toString()).toString());
					}
				}
			}
		} else {
			for (int i = 0 ; i<l.size() ; i++)
			{
				Map m = (Map)l.get(i);
				Iterator it = m.entrySet().iterator();
				int r =0;
				while(it.hasNext())
				{
					Map.Entry t = (Map.Entry) it.next();
					setCellValue(activeSheet , startCol +r , row +i  , t.getValue().toString());	
					r ++;
				}
			}
		}
	}
	
	
	public WritableSheet setSheetExcelValue(int index, int startRow, int startCol, String value)
	{
		WritableSheet r = null;
		
		r = this.getNewSheet(index);
		setCellValue(r, startRow, startCol, value);
		
		return r;
	}
	
	
	public WritableSheet newSheetExcelValue(String sheetName , int index , List l , List cap , List sort , int startRow , int startCol)
	{
		WritableSheet r = null;
		
		r = this.createSheet(sheetName, index);
		setExcelValue(r , l , cap ,sort, startRow , startCol);
		
		return r;
	}
	
	
	public WritableSheet newSheetExcelValue(String sheetName , List l , List cap , List sort , int startRow , int startCol)
	{
		WritableSheet r = null;
		
		r = this.createSheet(sheetName, 0);
		setExcelValue(r , l , cap , sort ,startRow , startCol);
		
		return r;
	}
	
	
	public WritableSheet newSheetExcelValue(String sheetName , List l , List cap ,List sort )
	{
		WritableSheet r = null;
		r = this.createSheet(sheetName, 0);
		setExcelValue(r , l , cap ,sort, 0 , 0);
		
		return r;
	}
	
	public WritableSheet newSheetExcelValue( List l , List cap , List sort)
	{
		WritableSheet r = null;
		r = this.createSheet("新建sheet1", 0);
		setExcelValue(r , l , cap ,sort , 0 , 0);
		
		return r;
	}
	
	public WritableSheet newSheetExcelValue( List l , List sort)
	{
		WritableSheet r = null;
		r = this.createSheet("新建sheet1", 0);
		setExcelValue(r , l , null ,sort, 0 , 0);
		return r;
	}
	
	public void saveQuit()
	{
		try {
			if (fWorkBook != null)
			{
				fWorkBook.write();
				fWorkBook.close();
				fWorkBook = null;
			}
			if (newReadBook != null)
			{
				newReadBook.write();
				newReadBook.close();
				newReadBook = null;
			}
			if (fReadBook != null)
			{
				fReadBook.close();
				fReadBook = null;
			}
		} catch (Exception e) {
			throw new SystemException(e.getMessage());
		}
		
	}
	public void withoutSaveQuit()
	{
		try {
			if (fWorkBook != null)
			{
				fWorkBook.close();
				fWorkBook = null;
			}
			if (newReadBook != null)
			{
				newReadBook.close();
				newReadBook = null;
			}
			if (fReadBook != null)
			{
				fReadBook.close();
				fReadBook = null;
			}
		} catch (Exception e) {
			throw new SystemException(e.getMessage());
		}
	}
  
}
</span>

  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 26
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xd聊架构

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值