`
hlbng
  • 浏览: 176659 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

导入导出Excel文件的方法

阅读更多

jsp页面(table):

<table id="table">
	<thead>
		<tr>
			<td align="center" nowrap="nowrap">
				流水号
			</td>

			<td align="center" nowrap="nowrap">
				<span style="font-size: 10pt">姓名</span>
			</td>
			<td align="center" nowrap="nowrap">
				<span style="font-size: 10pt">电话</span>
			</td>
			<td align="center" nowrap="nowrap">
				<span style="font-size: 10pt">手机</span>
			</td>
			<td align="center" nowrap="nowrap">
				<span style="font-size: 10pt">宅电</span>
			</td>
			<td align="center" nowrap="nowrap">
				<span style="font-size: 10pt">邮箱</span>
			</td>
			<td align="center" nowrap="nowrap">
				<span style="font-size: 10pt">联系组</span>
			</td>
			<td align="center" nowrap="nowrap">
				操作
			</td>
		</tr>
	</thead>
	<tbody>
		<%
			int i = 1;
		%>
		<logic:present name="stafflist">
			<logic:iterate id="staff" name="stafflist">
				<tr>
					<td align="center" nowrap="nowrap" style="height: 28px">
						<%=i++%></td>

					<td align="center" nowrap="nowrap" style="height: 28px">
						<bean:write name="staff" property="name" />
					</td>
					<td align="center" nowrap="nowrap" style="height: 28px">
						<bean:write name="staff" property="phone" />
					</td>
					<td align="center" nowrap="nowrap" style="height: 28px">
						<bean:write name="staff" property="tel" />
					</td>
					<td align="center" nowrap="nowrap" style="height: 28px">
						<bean:write name="staff" property="homephone" />
					</td>
					<td align="center" nowrap="nowrap" style="height: 28px">
						<bean:write name="staff" property="email" />
					</td>
					<td align="center" nowrap="nowrap" style="height: 28px">
						<bean:write name="staff" property="typecode" />
					</td>
				</tr>
			</logic:iterate>
		</logic:present>
	</tbody>
</table>

 导入导出事件:

<td align="center" nowrap="nowrap">
	<input type="file" id="importfile" name="importfile"
		onchange="importbook()"
		style="position: absolute; filter: alpha(opacity = 0); width: 80px; ime-mode: disabled;"
		onkeydown="return false" />
	<input type="button" value="导入联系人" style="width: 70px;">
</td>
<td align="center" nowrap="nowrap">
	<input type="button" value="导出联系人" onClick="exportbook()"
		style="width: 70px;">
</td>

 

function exportbook(){
	try  
	{
		var table=document.getElementById("table");
		var oXL = new ActiveXObject("Excel.Application");
		var oWB=oXL.Workbooks.Add();
		var oSheet=oWB.ActiveSheet;
		for (i=0; i < table.rows.length; i++)
	    {
		    for (j=0; j < table.rows(i).cells.length-2; j++)
		    {
		     oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j+1).innerText;
		    }
		    oXL.Visible = true;
		    oXL.UserControl = true;
	    }

	}     
	catch(e)   
	{   
	alert("您的电脑没有安装Microsoft Excel软件!")   
	return false  
	}   
}

  

function importbook(){
	
	var filePath = escape(encodeURIComponent($('importfile').value));
	var pattern=/^.+(\.xls[a-z]?)$|^.+(\.XLS[A-Z]?)$/;
	if(pattern.test(filePath)){
		var url = '/env2.0/importBookAction.do';
		var pars = 'method=importBook&filePath='+filePath;
		var ajax = new Ajax.Request(url,
	   		{method:'post',parameters:pars,onComplete:showcomplex}); 
	}else{
		alert("请选择excel文件!");
	}
}
function showcomplex(dataResponse){
	var data = eval('(' + dataResponse.responseText + ')');
	var issave = data.issave;
	if(issave){
		alert("导入成功");
	} else {
		alert("导入失败");
	}
	//页面刷新
	window.location.href="treelinktypeAction.do?method=findlinktype";
}

 importBookAction中的方法:

public ActionForward importBook(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String filePath = StringUtils.decodeStringByUTF8(request
				.getParameter("filePath"));
		System.out.println(filePath);
		// // 构造 HSSFWorkbook 对象,filePath 传入文件路径
		// HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
		// // 读取文件中的第一张表格
		// HSSFSheet sheet = wb.getSheetAt(0);
		// // 定义 row、cell
		// HSSFRow row;
		// // HSSFCell cell;
		// HSSFCell cell;
		// // 循环输出表格中的内容
		// System.out.println("+++" + sheet.getFirstRowNum()
		// +"+++"+sheet.getPhysicalNumberOfRows());
		// for (int i = sheet.getFirstRowNum() + 1; i <
		// sheet.getPhysicalNumberOfRows(); i++) {
		// row = sheet.getRow(i);
		// for (short j = row.getFirstCellNum(); j <
		// row.getPhysicalNumberOfCells(); j++) {
		// // 推荐通过 row.getCell(j).toString() 获取单元格内容,
		// cell = row.getCell(j);
		// if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
		// System.out.println((int) cell.getNumericCellValue());
		// } else if (cell.getCellType() == cell.CELL_TYPE_STRING) {
		// System.out.println(cell.getStringCellValue());
		// }
		// }
		// }

		List l = new ArrayList();
		Workbook book = Workbook.getWorkbook(new File(filePath));
		// 获得第一个工作表对象
		for (int i = 0; i < book.getNumberOfSheets(); i++) {
			Sheet sheet = book.getSheet(i);
			for (int j = 1; j < sheet.getRows(); j++) {
				StringBuffer sb = new StringBuffer("");
				for (int k = 0; k < sheet.getColumns(); k++) {
					Cell cell = sheet.getCell(k, j);
					String result = cell.getContents();
					sb.append(result + ",");
				}
				l.add(sb.toString());
			}
		}
		
		List<Temailbook> booklist = new ArrayList<Temailbook>();
		Integer no = temailbookDao.findbookAllNo();
		Tusers user = (Tusers) request.getSession().getAttribute("user");
		Temailbook temailbook = null;
		System.out.print(l.size());
		for (int i = 0; i < l.size(); i++) {
			String s = (String) l.get(i);
			String[] ss = s.split(",");
			String typecode = tlinktypeDao.findcodeByname(ss[5]);
			
			temailbook = new Temailbook();
			temailbook.setCode(no.toString());
			temailbook.setNo(Long.valueOf(no));
			temailbook.setStaffcode(user.getCode());
			temailbook.setName(ss[0]);
			temailbook.setTypecode(typecode);
			temailbook.setPhone(ss[1]);
			temailbook.setTel(ss[2]);
			temailbook.setHomephone(ss[3]);
			temailbook.setEmail(ss[4]);
			temailbook.setMemo(null);
			temailbook.setIsdel(Long.valueOf(SingleObjectCollect.isdel_new));
			
			booklist.add(temailbook);
			no++;
		}
		
		boolean issave = temailbookDao.savebook(booklist);
		response.setContentType("text/html; charset=GBK");
		try {
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("issave", issave);
			out.print(obj.toString());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.print("异常");
		}
		return null;
	}

 

分享到:
评论

相关推荐

    java Swing 导入导出Excel文件

    自定义的一个excel文件导入导出类,支持Excel2003到2010的格式。里面也有简单的配置文件的创建。

    导入导出Excel方法,很详细

    总结来说,导入导出Excel是数据处理的重要环节,涉及到对Excel文件的操作,包括读取、写入和格式设置。选择合适的库,创建有效的模板,以及使用预先封装好的工具类,都能帮助我们高效地完成这项任务。在实际项目中,...

    C# Winform实现导入和导出Excel文件

    本文实例为大家分享了Winform实现导入导出Excel文件的具体代码,供大家参考,具体内容如下 /// /// 导出Excel文件 /// /// /// &lt;param name=dataSet&gt;&lt;/param&gt; /// 数据集 /// 导出后是否打开文件 /// ...

    SSM框架利用poi导入导出Excel文件 demo

    在本示例"SSM框架利用poi导入导出Excel文件 demo"中,我们将探讨如何在SSM项目中使用Apache POI库来实现Excel文件的导入与导出功能。 Apache POI是一个流行的开源库,它允许开发者在Java应用程序中创建、修改和显示...

    NPOI导入导出Excel源文件,自己整理的,非常好用

    这个资源包“NPOI导入导出Excel源文件”显然是一个实用的工具集,帮助开发者轻松地处理Excel数据的导入与导出。 1. **NPOI介绍** NPOI是.NET平台上的一个开源项目,它提供了读写Excel文件的功能。NPOI支持两种主要...

    MVC开发的导入导出Excel文件

    导出Excel文件涉及到从数据库或内存中的数据生成新的Excel文件。这个过程与导入类似,但方向相反。 1. **创建Excel工作簿**:使用EPPlus创建一个新的ExcelPackage对象,然后添加工作簿和工作表。 2. **填充数据**...

    jqGrid控件导入导出Excel文件

    jqGrid控件导入导出Excel文件,将几个jqgrid导出一份Excel多sheet文件 ,亦可多sheet的excel导入至多个jqGrid表格。

    jsp导入导出excel文件

    在Java Web开发中,JSP(JavaServer Pages)经常用于创建动态网页,而与之配合进行Excel文件导入导出的功能则常使用Apache POI库。Apache POI是Apache软件基金会的一个开源项目,提供了处理Microsoft Office格式文件...

    servlet导入导出excel

    在这个场景中,我们讨论的是如何使用Servlet和JSP(JavaServer Pages)来处理Excel文件的导入和导出,尤其是与MySQL数据库的交互。Excel文件有两种主要格式:XLS(Excel 97-2003工作簿)和XLSX(Excel 2007及以后...

    QT 高效 导入导出excel , tableWidget显示

    以上就是QT高效导入导出Excel并使用tableWidget显示的基本流程和优化策略。实际开发时,根据项目需求和资源限制,可能还需要进行额外的错误处理和功能完善。这个程序虽然不完善,但提供了清晰的开发思路,有助于快速...

    Excel文件导入导出

    总结,Excel文件的导入导出涉及到多种技术和方法,包括编程语言中的库、VBA宏和.NET Framework等。通过合理的封装和优化,可以高效、安全地处理大量数据的导入导出需求。在实际应用中,根据项目特性和需求选择合适的...

    vue导入导出excel文件

    在这个场景中,提到的"vue导入导出excel文件"很可能是通过JavaScript库来完成的,因为标签中提到了"js"。 JavaScript库如`xlsx`或`file-saver`可以帮助我们实现Excel文件的处理。`xlsx`库用于读取、创建和修改Excel...

    基于NPOI导入导出Excel文件.rar

    总结,本项目展示了如何结合C#、SQL和NPOI库来实现高效的数据导入导出功能,通过泛型方法提高了代码的复用性,适应不同的数据类型。对于需要处理大量Excel数据与SQL数据库交互的场景,这是一个非常实用的技术解决...

    纯前端:luckysheet在线编辑Excel导出,Excel文件导入

    至于Excel文件的导入导出,Luckysheet支持读取和写入CSV、JSON以及Excel的XLSX格式文件。用户可以通过选择文件或者拖拽的方式,将本地的Excel文件上传到网页上,然后在Luckysheet中进行查看和编辑。反之,编辑后的...

    linux下数据库导入导入导出EXCEL文件

    ### Linux环境下SQLite数据库与Excel文件的数据交互 #### 一、引言 在Linux环境中,SQLite是一种广泛使用的轻量级数据库管理系统,它具有简单易用、跨平台等特性,非常适合用于小型项目或个人开发中。然而,在实际...

    MVC导入与导出excel文件

    在MVC架构下,处理导入和导出Excel文件是常见的需求,尤其在数据管理、报表生成以及数据交换场景中。下面我们将详细探讨如何在MVC应用中实现这个功能。 首先,导入Excel文件涉及到读取Excel数据并将其转化为可操作...

    WPF中对Excel文件的导入导出

    在WPF(Windows Presentation Foundation)应用开发中,与Excel文件的交互是常见的需求,比如导入数据到Excel,或者从Excel导出数据。本教程将详细讲解如何实现一个包含"导入"和"导出"功能的界面,并展示数据在一个...

    SSM框架导入导出Excel文件

    SSM框架配合Apache POI,能够高效地处理Excel数据的导入导出,使得Java Web应用能够与Excel文件进行便捷的数据交换。在实际应用中,需要注意性能优化、错误处理和用户体验,以提供稳定、易用的服务。

    .net导入导出Excel文件

    在.NET开发环境中,处理Excel文件是一项常见的任务,无论是数据导入还是导出,都有很多方法可以实现。本篇文章将深入探讨如何使用NPOI这个开源库来处理Excel文件,并结合DataTable进行数据交换。 NPOI是一个.NET...

    SpringBoot +Mybatis +POI导入、导出Excel文件

    - 创建一个`ExcelExportController`,其中`exportExcel`方法负责查询数据并导出Excel。 - 注意处理异常,例如文件上传失败、数据格式不匹配、数据库操作失败等。 6. **模板文件与数据库脚本** - 提供的`doc`目录...

Global site tag (gtag.js) - Google Analytics