.net Excel文档 的导出

printAddressList.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PrintAddressList.aspx.cs" Inherits="test_PringAddressList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>

<script language="javascript" type="text/javascript">

function btnExport_onclick() {
hideIframe.location.href = "/Web/handler/ExportXlsHandler.ashx?temp=" + (new Date()).getTime();
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<input id="btnExport" type="button" value="导出通讯录" onclick="return btnExport_onclick()" /><br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="odsUserInfo"
Width="572px">
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
<asp:BoundField DataField="DutyInfoNo" HeaderText="DutyInfoNo"
SortExpression="DutyInfoNo" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="Mobile" HeaderText="Mobile"
SortExpression="Mobile" />
<asp:BoundField DataField="QQ" HeaderText="QQ" SortExpression="QQ" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="DeptInfoNo" HeaderText="DeptInfoNo"
SortExpression="DeptInfoNo" />
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
<asp:ObjectDataSource ID="odsUserInfo" runat="server"
SelectMethod="GetUserInfo" TypeName="ExtOA.Biz.UserInfoBiz">
</asp:ObjectDataSource>

</div>
<iframe name="hideIframe" id="hideIframe" src="" width="0" height="0" scrolling="no" frameborder="0" style="display:none"></iframe>
</form>
</body>
</html>

ExportXlsHandler.ashx :

<%@ WebHandler Language="C#" Class="ExportXlsHandler" %>
using System;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Web;
using System.Web.SessionState; // 要使用 Session 必需加入此命名空间
using System.Web.Configuration;
using System.Collections.Generic;
using ExtOA.Biz;
using ExtOA.Ent;

public class ExportXlsHandler : IHttpHandler, IRequiresSessionState
{
string mErr = "", tp_title = "国讯教育员工通讯录", tfile = "", sfile = "", wpath = "";
string BranchName1 = (new BranchInfoBiz()).GetBranchInfoById(1).BranchName;
string BranchName2 = (new BranchInfoBiz()).GetBranchInfoById(2).BranchName;

/// <summary>
/// 保存数据(添加或修改数据)
/// </summary>
/// <param name="oledb_conn">conn</param>
/// <param name="sheetName">工作表名称</param>
/// <param name="type">insert|update</param>
private void SaveData(OleDbConnection oledb_conn, string sheetName, string type)
{

using (OleDbCommand oledb_command = new OleDbCommand())
{
string SqlString = "";

oledb_command.Connection = oledb_conn;

#region 更新‘通讯录名称’
SqlString = "Update [" + sheetName + "$] Set remark = @remark Where remark = ‘名称‘;";

oledb_command.CommandText = SqlString;
oledb_command.Parameters.Clear();
oledb_command.Parameters.AddWithValue("remark", string.Format("国讯教育通讯录({0})", sheetName));
oledb_command.ExecuteNonQuery();
#endregion

#region 更新‘制表日期’
SqlString = "Update [" + sheetName + "$] Set remark = @remark Where remark = ‘制表日期‘;";

oledb_command.CommandText = SqlString;
oledb_command.Parameters.Clear();
oledb_command.Parameters.AddWithValue("remark", "制表日期:" + DateTime.Now.ToString("yyyy/MM/dd"));
oledb_command.ExecuteNonQuery();
#endregion
IList<UserInfo> userInfos = (new UserInfoBiz()).GetUserInfoByDeptInfoId(1);//根据机构ID得到该机构所有员工列表
int index = 0;
foreach (UserInfo userInfo in userInfos)
{
index++;
if (type == "insert")
SqlString = "insert into [" + sheetName + "$](remark,depart,username,dutyname,mobile,qq,email) values(@remark, @depart, @username, @dutyname, @mobile, @qq,@email)";
else
SqlString = "Update [" + BranchName2 + "$] set remark = @remark,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where remark=‘" + index + "‘";
oledb_command.CommandText = SqlString;
oledb_command.Parameters.Clear();
oledb_command.Parameters.AddWithValue("remark", " " + index.ToString() + " ");
string DeptName = (new DepartInfoBiz()).GetDepartInfoById(userInfo.DeptInfoNo).DepartName;
oledb_command.Parameters.AddWithValue("depart", DeptName);
oledb_command.Parameters.AddWithValue("username", userInfo.UserName);
string dutyName = (new DutyInfoBiz()).GetDutyInfoById(userInfo.DutyInfoNo).DutyName.Split(‘-‘)[1];
oledb_command.Parameters.AddWithValue("dutyname", dutyName);
oledb_command.Parameters.AddWithValue("mobile", userInfo.Mobile);
oledb_command.Parameters.AddWithValue("qq", userInfo.QQ);
oledb_command.Parameters.AddWithValue("email", userInfo.Email);

oledb_command.ExecuteNonQuery();
}
}
}

public void ProcessRequest(HttpContext context)
{
#region 复制 Excel 文件,以解决共用问题
wpath = context.Server.MapPath("~/xls/Default/");
sfile = wpath + "addresstemplate.xls";

wpath = context.Server.MapPath("~/xls/Excel/");
tfile = wpath + string.Format("国讯教育员工通讯录({0}{1}).xls", DateTime.Now.Year.ToString("D2"), DateTime.Now.Month.ToString("D2"));

File.Delete(tfile);
if (File.Exists(tfile))
mErr = "文件无法创建!\\n";
else
File.Copy(sfile, tfile, true);
#endregion

#region 打开新的 Excel 文件
if (mErr == "")
{

// Excel的第一列为字段名称
string xls_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ tfile + ";Extended Properties=\"Excel 8.0;HDR=YES\"";

// Excel的第一列没有字段名称
// string xls_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tfile + ";Extended Properties=\"Excel 8.0;HDR=NO\"";

using (OleDbConnection oledb_conn = new OleDbConnection(xls_conn))
{
oledb_conn.Open();
SaveData(oledb_conn,BranchName1,"insert");//对上海校区进行操作
SaveData(oledb_conn,BranchName2,"update"); //对北京校区进行操作
oledb_conn.Close();
string filename = string.Format("../xls/Excel/国讯教育员工通讯录({0}{1}).xls", DateTime.Now.Year.ToString("D2"), DateTime.Now.Month.ToString("D2"));
context.Response.Redirect(filename + "?xtemp=" + DateTime.Now.ToString("HHmmss"));

}
#endregion

if (mErr != "")
{
// 设置输出格式
context.Response.ContentType = "text/html";
context.Response.Write("<script type=\"text/javascript\">alert(\"" + mErr + "\");/script>");
context.Response.End();
}

}
}
public bool IsReusable {
get {
return false;
}
}

}

时间: 2024-12-14 07:06:02

.net Excel文档 的导出的相关文章

支持将数据导出到Excel文档的时候设置单元格格式的.NET控件Spire.DataExport

Spire.DataExport for .NET是e-iceblue公司推出的一款数据导出类.NET控件.作为一款专业的数据导出控件,Spire.DataExport for .NET可以帮助开发人员轻松快速的从各种主流数据库中导出数据并存储于各种文件格式中.他支持从SQL Command, DataTable,ListView中导出数据并存储于MS Excel,MS Word, HTML, XML, PDF, MS Access, DBF, SQL Script, SYLK, DIF, CS

利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出

我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的博客介绍过几篇关于Aspose.Word控件和Aspose.Cell控件的使用操作,如下所示. <使用Aspose.Cell控件实现Excel高难度报表的生成(一)> <使用Aspose.Cell控件实现Excel高难度报表的生成(二)> <使用Aspose.Cell控件实现Ex

Apache POI -- Java 导出Excel文档(笔记)

一.Action类 import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Met

POI导出EXCEL文档

package com.wiseweb.util.excel; import java.io.*; import java.util.*; import javax.swing.JOptionPane; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; import com.wiseweb.pom.entity.BaiinfoPriceTime; public class Expo

struts2中利用POI导出Excel文档并下载

1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: 1 package com.tydic.eshop.action.feedback; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import

Asp.net的对Excel文档的导入导出操作

刚刚初入职场,在休闲的时间写下了项目中用到的对Excel文档操作的方法以及总结,多的不说,直接上代码 public static void CreateExcel(DataSet ds, string FileName) { //resp = Page.Response; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Ch

Asp.net中导出Excel文档(Gridview)

主要思路,通过GridView来导出文档. 新建一个Aspx页面,页面创建GridView控件,后台绑定好数据源.然后load中直接打印即可导出 前台的GridView <asp:GridView ID="GridView1" BorderColor="Black" runat="server" AutoGenerateColumns="False" Font-Size="12px" Width=&q

mac OS X:将CSV格式转换为Excel文档格式

一:在Mac上如果你使用Excel打开windows导出的CSV格式文档,你会发现表格中所有的的内容都显示在A列.那么,如何恢复正常呢,你可以将CSV格式的文档导入到Excel文档中,这样就正常显示了. 1. 2. 3. 4. 5. 6. 7. 8. 9. 二:中文乱码解决方法 使用 Numbers软件打开文档,然后 ps: 其实可以不用Excel的,直接使用Numbers就足够了,编辑和导出. 版权声明:本文为博主原创文章,未经博主允许不得转载.

WPF-两份excel文档列自动匹配导入工具-技术&amp;分享

WPF-两份excel文档列自动匹配导入工具-技术&分享 A文档中包含两列x,y(x与y对应):B文档包含一列y,需要将A文档的y匹配B文档的y,将A文档的x内容匹配到B文档中,与B文档中的y列对应. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows; using System.Windows.Forms; using Mysoft.Co