本文引用以下Microsoft.NET Framework类库命名空间:
-
System.Data.SqlClient
-
System.IO
-
System.Text
本任务的内容
摘要
本文分步介绍如何从数据库动态生成制表符分隔的文本文件。 然后,可以在 Microsoft Excel 中打开该文件。 本文中的示例代码演示如何连接到Microsoft SQL Server数据库,从 Pubs 数据库返回一组数据,然后使用数据创建制表符分隔的文本文件。
生成示例代码
此示例 ASP.NET 名为 ExcelCS.aspx 的 Visual C# .NET 页创建一个示例。 此页面连接到 SQL Server Pubs 数据库,并使用 FileStream 对象将信息返回到制表符分隔的文本文件。 然后,“ASP.NET”页会显示一个指向 .xls 文件的链接,该文件是你为演示代码输出而创建的。
-
Microsoft Visual Studio .NET 启动。
-
在“文件”菜单上,指向“新建”,然后单击“项目”。
-
在“新建项目”对话框中的“项目类型”下,单击“Visual C# 项目”。 在“模板”下,单击“ASP.NET Web 应用程序”。
-
在“位置”框中,使用以下格式键入服务器名称和项目名称:
http:// ServerName/项目名称 在此示例中,将项目命名为 ExcelCSTest。 如果使用本地服务器,可以将服务器名称保留为http://localhost。
-
将 HyperLink 控件从工具箱拖动到 WebForm1.aspx 文件。
-
右键单击“WebForm1.aspx”,然后单击“查看代码”以显示代码隐藏页源。
-
将以下语句添加到代码隐藏页的顶部:
using System.Data.SqlClient;using System.IO;using System.Text;
-
在 WebForm1.aspx 的代码隐藏页中,将以下代码添加到 Page_Load 事件:
//You use these variables throughout the application.string fileExcel, filePath, fileName, strLine, sql;FileStream objFileStream;StreamWriter objStreamWriter;Random nRandom = new Random(DateTime.Now.Millisecond);SqlConnection cnn = new SqlConnection("server=(local);database=pubs;Integrated Security=SSPI"); //Create a random file name.fileExcel = "t" + nRandom.Next().ToString() + ".xls";//Set a virtual folder to save the file.//Make sure to change the application name to match your folder.filePath = Server.MapPath("\\ExcelCSTest");fileName = filePath + "\\" + fileExcel;//Use FileSystem objects to create the .xls file.objFileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); objStreamWriter = new StreamWriter(objFileStream); //Use a DataReader object to connect to the Pubs database.cnn.Open();sql = "select au_id,au_lName,au_fname,phone,address,city,state,zip,contract from authors"; SqlCommand cmd = new SqlCommand(sql, cnn); SqlDataReader dr; dr = cmd.ExecuteReader();//Initialize the string that is used to build the file.strLine = "";//Enumerate the field names and the records that are used to build //the file.for (int i = 0; i <= dr.FieldCount-1; i++) { strLine = strLine + dr.GetName(i).ToString() + Convert.ToChar(9); }//Write the field name information to the file.objStreamWriter.WriteLine(strLine);//Reinitialize the string for data.strLine = "";//Enumerate the database that is used to populate the file.while (dr.Read()) { for (int i = 0; i <= dr.FieldCount-1; i++) { strLine = strLine + dr.GetValue(i).ToString() + Convert.ToChar(9); } objStreamWriter.WriteLine(strLine); strLine=""; }//Clean up.dr.Close();cnn.Close();objStreamWriter.Close();objFileStream.Close();//Include a link to the Excel file.HyperLink1.Text="Open Excel";HyperLink1.NavigateUrl=fileExcel;
-
根据需要修改连接字符串 (SqlConnection) 。
-
在“文件”菜单上,单击“全部保存”以保存项目文件。
-
在 Visual Studio .NET 集成开发环境 (IDE) 的“生成”菜单上,单击“生成解决方案”以生成项目。
-
在 Visual Studio .NET 集成开发环境解决方案资源管理器中,右键单击“WebForm1.aspx”,然后单击“在浏览器中查看”以运行代码。
故障 排除
-
必须更改代码示例中的连接字符串以匹配环境。
-
你可能必须在 Microsoft Windows 2000 和 Microsoft Windows XP) 或 Microsoft Windows Server 2003) 中的 w3wp 进程 (中增加aspnet_wp进程 (的权限,才能允许写入文件。 有关其他信息,请单击下面的文章编号以查看Microsoft知识库中的文章:
317012 INFO:在 ASP.NET 中处理和请求标识