Posted by scmay on Thu 6 Aug 04:28
report abuse | download | new post
- private void SaveToExcel(IList personList)
- {
- //Make sure folder where the template lies has full permission
- //This lets you know what user is running the system
- Debug.Print(Environment.UserDomainName + "\\" + Environment.UserName);
- string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + System.Configuration.ConfigurationManager.AppSettings["ExcelPath"] + ";Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34;
- if (File.Exists(System.Configuration.ConfigurationManager.AppSettings["ExcelFilePath"]))
- File.Delete(System.Configuration.ConfigurationManager.AppSettings["ExcelFilePath"]);
- DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
- using (DbConnection connection = factory.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- connection.Open();
- using (DbCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE TABLE Person (ID char(255), Name char(255), Age char(255))";
- command.ExecuteNonQuery();
- foreach(IPerson person in personList )
- {
- command.CommandText = "INSERT INTO Person (ID, Name, Age ) VALUES('" + person.ID + "','" + person.Name + "','" + person.Age + "') ";
- command.ExecuteNonQuery();
- }
- }
- connection.Close();
- }
- view.Write("<script language=\"Javascript\">window.open('OpenFile.aspx?fn=" + System.Configuration.ConfigurationManager.AppSettings["ExcelFilePath"].Replace("\\", "\\\\") +
- "',\"ExcelExport\");</script>");
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.