当前位置:首页 > 代码 > 正文

asp写入exl源代码(asp生成excel)

admin 发布:2022-12-19 15:26 128


本篇文章给大家谈谈asp写入exl源代码,以及asp生成excel对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

asp中如何导入Excel模板, 求代码

public class PrinExcel

{

public PrinExcel()

{

//

//TODO: 在此处添加构造函数逻辑

//

}

public static void ToExcel(System.Web.UI.Control ctl, string strFileName)

{

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");

HttpContext.Current.Response.Charset = "utf-8";

string style = @"style .text /script "; //Excel中的文本格式

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

//System.Text.Encoding.Default;

HttpContext.Current.Response.ContentType = "application/ms-excel"; //设置输出流为简体中文

ctl.Page.EnableViewState = false;

System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

ctl.RenderControl(hw);

HttpContext.Current.Response.Write(style);

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

hw.Flush();

hw.Close();

tw.Flush();

tw.Close();

}

//导出到Excel

public static void ToExcel2(System.Web.UI.Control ctl, string strFileName)

{

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");

HttpContext.Current.Response.Charset = "GB2312"; //"utf-8";

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

//System.Text.Encoding.Default;

HttpContext.Current.Response.ContentType = "application/ms-excel"; //设置输出流为简体中文

ctl.Page.EnableViewState = false;

System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

ctl.RenderControl(hw);

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

hw.Flush();

hw.Close();

tw.Flush();

tw.Close();

}

//导出到Excel

public static void ToExcel(DataTable dt)

{

string sb = "";

foreach (DataRow dr in dt.Rows)

{

for (int i = 0; i dt.Columns.Count; i++)

{

sb = sb + dr[i].ToString() + "\t";

}

sb = sb + "\n";

}

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=myexcel.xls");

HttpContext.Current.Response.Charset = "UTF-8";

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;

HttpContext.Current.Response.ContentType = "application/ms-excel";

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

hw.WriteLine(sb.ToString());

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

hw.Flush();

hw.Close();

tw.Flush();

tw.Close();

}

//导出到Excel

public static void ToExcel(string sb)

{

HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline;filename=myexcel.xls");

HttpContext.Current.Response.Charset = "UTF-8";

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;

HttpContext.Current.Response.ContentType = "application/ms-excel";

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

hw.WriteLine(sb.ToString());

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

hw.Flush();

hw.Close();

tw.Flush();

tw.Close();

}

在你需要的事件中调用就Ok了

asp网页中如何导入excel

两种方法

1.拿execl当做数据库来读取

2.可以把excel另存为网页文件,就可以当做文本文件处理了,里面代码可以找到处理规律。

附上处理excel文档的asp代码(就是第一条的方法)

set conn2=CreateObject("ADODB.Connection")

conn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended properties=Excel 5.0;Data Source="file_path""

sql="select*FROM [student$]"

set rs=conn2.execute(sql)

do while not rs.eof

sql="insert into student([student_name],[student_nick],[student_password])values('"fixsql(rs(0))"','"fixsql(rs(3))"','"fixsql(rs(8))"')"

conn.execute(sql)

rs.movenext

loop

conn2.close

如何用asp读取Excel文件

下面是读取一个EXCEL文件并将其写入数据库的代码实例,亲测通过:

注意:EXCEL里面的列名称要和数据库里的字段相同

%

i=0

Dim cn,oConn,connstr

'打开XLS.

Set cn = Server.CreateObject("ADODB.Connection")

cn.Provider = "Microsoft.Jet.OLEDB.4.0 "

cn.ConnectionString = "Data Source=" Server.MapPath("list.xls") ";" _

"Extended Properties=Excel 8.0;"

cn.Open

'打开MDB.

connstr="DBQ="+server.MapPath("TEST.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"

set oConn=server.CreateObject("ADODB.CONNECTION")

oConn.open connstr

'读取数据.

set rsRead=server.CreateObject("ADODB.Recordset")

rsRead.Open "select * from [list$]",cn,1,1

do until rsRead.EOF

'写入数据库.

oConn.Execute("Insert into eer(mc)Values('" rsRead.Fields("mc") "')" )

rsRead.MoveNext

i=i+1

loop

response.write("p align=center成功导入"i"条数据/pbr")

response.Write("p align=centera href=javascript:window.close()关闭窗口/a/p")

%

asp怎么将数据导入excel

你说的应该是导出到.xls文件吧.

有个简单取巧的方法,就是把你的数据每列用TAB分隔,每行用换行分隔,以纯文本形式导出,保存是扩展名为.xls,这样就可以用Excel直接打开. 此法虽然方便,但缺点也很明显,就是无法指定格式.

例如:

姓名 性别 出生日期

张三 男 1980-1-1

李四 女 1989-12-31

保存为 t.xls 就可以直接用Excel打开了.

高级一点的方法还有,你可以在网上找一个 RecordsetConverter.sct 文件,这个也是无组件导出到Excel的.这个支持格式,而且使用简单.

最麻烦的当然就是直接在服务端调用Excel.Application组件了,也能实现,但代码一般都很长,而且不容易修改.

如何将ASP中的SQL数据库中查询出来的某一数据写入到EXCEL

你这样做只能生成.CSV文件,EXCEL软件一样能打开、编辑。

真正的.XLS文件无法自动生成,因为.xls文件的建立需要office的支持,并且有版本区别。你可以先手工建立个.xls文件,然后用FSO复制文件进行编辑。

不同点:

CSV文件可以直接用FSO进行编辑,xls文件需要ADO进行编辑。使用的控件不同。

CSV文件字段长度限制较松,xls文件低版本字段长度限制较紧。

CSV文件受逗号影响,xls文件不受逗号影响。

CSV文件需要遍历整个文件查询字段,XLS使用SQL指令查询字段。

CSV文件需要暂存数据重新写入才能编辑文件,XLS使用SQL指令就能编辑字段。

CSV文件服务器无需安装office,XLS文件服务器必须装有office。

我在单位也用CSV+ASP做了个页面,用来自动识别IP及管理服务器文件共享等,并提供对应主机的配置文件下载。个人感觉CSV做表格导入导出及当数据仓库很好,在服务器支持的情况下access当缓冲数据库更快。但在服务器存储中,建议更改后缀,防止被恶意下载。

asp写入exl源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于asp生成excel、asp写入exl源代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/10286.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载