作者:乔山办公网日期:
返回目录:excel表格制作
如果Datatable数据来源于数抄据库,可以:
Dim sql$ = "SELECT * INTO [Excel 8.0;database=" & sTargetPath & "fileName.xls" & "].[Sheet1] FROM tableName"
Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
其它情况,遍历datatable逐个写入excel单元格zhidao
使用table2excel表格插件需要在页面中引入jquery和jquery.table2excel.js文件。
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.table2excel.js"></script>
HTML结构
你可以将任何标准的e799bee5baa6e59b9ee7ad94365HTML表格结构的数据导出到Excel中,一个HTML表格的结构为:
<table>
<thead>
<tr class="noExl">
<td>带<code>noExl</code>class的行不会被输出到excel中</td>
<td>带<code>noExl</code>class的行不会被输出到excel中</td>
</tr>
<tr>
<td>这一行会被导出到excel中</td>
<td>这一行会被导出到excel中</td>
</tr>
</thead>
<tbody>
<tr>
<td>单元格1-1</td>
<td>单元格1-2</td>
</tr>
<tr>
<td>单元格2-1</td>
<td>单元格2-2</td>
</tr>
<tr>
<td>单元格3-1</td>
<td>单元格3-2</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">合并2个单元格</td>
</tr>
</tfoot>
</table>
如果表格中的某一行不需要导出到Excel中,可以为这一行添加.noExl class类,该class类会在插件初始化时通过参数被指定为不被导出的数据。
初始化插件
在页面DOM元素加载中完毕之后,可以通过下面的方法来初始化table2excel插件。
$("#table2excel").table2excel({
// 不被导出的表格行的CSS class类
exclude: ".noExl",
// 导出的Excel文档的名称
name: "Excel Document Name",
// Excel文件的名称
filename: "myExcelTable"
});
配置参数
table2excel插件的可用配置参数有:
exclude:不被导出的表格行的CSS class类。
name:导出的Excel文档的名称。
filename:Excel文件的名称。
exclude_img:是否导出图片。
exclude_links:是否导出超链接
exclude_inputs:是否导出输入框中的内容。
下面的函数作用,将DataTable导出到EXCEL文件:
private void DataTabletoExcel(System.Data.DataTable tmpDataTable,string strFileName)
{
if (tmpDataTable == null)
{
return;
}
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Excel.Application xlApp = new Excel.ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
//将DataTable的列名导e799bee5baa6e79fa5e98193e78988e69d83336入Excel表第一行
foreach(DataColumn dc in tmpDataTable.Columns)
{
columnIndex ++;
xlApp.Cells[rowIndex,columnIndex] = dc.ColumnName;
}
//将DataTable中的数据导入Excel中
for(int i = 0;i<rowNum; i++)
{
rowIndex ++;
columnIndex = 0;
for (int j = 0;j<columnNum; j++)
{
columnIndex ++;
xlApp.Cells[rowIndex,columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
xlBook.SaveCopyAs(strFileName + ".xls");
}
本篇教程我们会看到如何把JSP页面导出到e68a84e8a2ade79fa5e98193330Excel中,会在已有的JSP页面中增加导出excel的功能。
许多时候对于用户来说,可以在excel中看到页面内容是很方便的。公共的方案会被导出成包含一些报告、数字等信息的表格。通过导出数据导出到excel中,最终用户也可以使用excel来做各种的分析,这一点对于你的java基本程序来实现,是有困难的。
这是对应的jsp源码(导出excel功能还没有加)。一个包含简单数据表格的jsp页面。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>
<table align="center" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
</body>
</html>
我们会添加一个“导出到excel”的超链接,它会把页面内容导出到excel文件中。那么这个页面会变成上图。
下面是新版本的jsp源码。这个版本增加了“导出到excel”超链接,而且增加了相应的功能:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>
<%
String exportToExcel = request.getParameter("exportToExcel");
if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "excel.xls");
}
%>
<table align="left" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
<%
if (exportToExcel == null) {
%>
<a href="excel.jsp?exportToExcel=YES">Export to Excel</a>
<%
}
%>
</body>
</html>