1
Answer

CReating an Excel spreadsheet File without opening Excel

Mark Slater

Mark Slater

16y
3.7k
1

I've read a couple of articles about using C# to open and read an Excel spreadsheet by opening a spreadsheet via the code. Is there a way of writing a Excel file without having to open the spreadsheet.

We have a requirement to read a set of data from a database, put this into an excel spreadsheet so that the data can be used our Marketing department. The database sits at our external hosting site and the current process is to create a comma delimited file, import it into Excel, format and save. All a bit clunky and MANUAL!

I would like to set up a scheduled process, that can run automomously, as abackground process without opening an Excel form. Can this be done?

Answers (1)
0
Satyapriya Nayak

Satyapriya Nayak

NA 53k 8m 13y
Hi Sumaira,


Try this...


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    string str;
    SqlCommand com;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        SqlConnection con = new SqlConnection(strConnString);

        if (!IsPostBack)
        {
            DropDownList1.Items.Add("Choose");
            con.Open();
            str = "select * from clinic patient registration";
            com = new SqlCommand(str, con);
            SqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                DropDownList1.Items.Add(reader["patient reg code"].ToString());
            }
            reader.Close();
            con.Close();
        }
    }
  
}



Thanks
Accepted
0
thanks sir