本文參照下列 Microsoft .NET Framework 類別庫的命名空間:
-
System.Data.SqlClient
-
System.IO
-
系統.文本
本文內容
摘要
本文逐步說明如何從資料庫動態建立制表符分隔文字檔。 接著你可以用 Microsoft Excel 開啟這個檔案。 本文範例程式碼示範如何連接 Microsoft SQL Server 資料庫,從 Pubs 資料庫回傳一組資料,然後建立一個以制表符分隔的文字檔。
建立範例程式碼
此範例建立一個名為 ExcelCS.aspx ASP.NET Visual C# .NET 頁面範例。 此頁面連接 SQL Server Pubs 資料庫,並使用 FileStream 物件將資訊回傳為以制表符分隔的文字檔。 ASP.NET 頁面會顯示你建立的 .xls 檔案連結,用來展示程式碼的輸出。
-
啟動 Microsoft Visual Studio .NET。
-
在檔案選單中,指向「新建」,然後點選專案。
-
在新專案對話框中,專案類型中,點選 Visual C# 專案。 在範本中,點擊 ASP.NET 網頁應用程式。
-
在位置欄位輸入伺服器名稱與專案名稱,格式如下:
http:// ServerName/Project Name 在這個範例中,將專案命名為 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,然後點擊瀏覽器檢視以執行程式碼。
故障排除
-
你必須將程式碼範例中的連接字串更改,以符合你的環境。
-
你可能需要提高 aspnet_wp 程序的權限, (Microsoft Windows 2000 和 Windows XP) Microsoft Microsoft Windows Server 2003) 中的 w3wp 程序 (,才能寫入該檔案。 如需詳細資訊,請按一下下面的文章編號,檢視「Microsoft 知識庫」中的文章:
317012 資訊:ASP.NET 中申請並申請身份