You must Sign In to post a response.
  • Category: .NET

    How to download the data in web page to openoffice spreadsheet

    In my application i have to read the open office excel spread sheet data and save the data that is present in web page to open office spread sheet.Open office Excel sheet data is read and displayed in grid view but the messages are not displayed in grid view and when i click on save button only the data in spread sheet gets downloaded but not Below is the code that i used but it saves only the data that is spread sheet but i need to download the msgs also along with it how can i do this
    <div>
    <asp:FileUpload ID="FileUpload1" runat="server"/>
    <asp:Button ID="Button1" runat="server" Text="ReadFile" OnClick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="Refresh" OnClick="Button2_Click" />
    <asp:Button ID="BtnSave" runat="server" Text="Save" onclick="BtnSave_Click" />
    <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click" Text="Insert Records" />
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowHeader="False" Height="114px" Width="182px">
    </asp:GridView>
    <asp:Label ID="UploadStatusLabel" runat="server"></asp:Label>
    </div>

    </div>

    string connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd;
    string msg = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
    UploadStatusLabel.Text = "";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    try
    {
    string fileName = SaveToAppFolder(FileUpload1.PostedFile);
    OdsReaderWriter obj = new OdsReaderWriter();
    DataSet dset = obj.ReadOdsFile(fileName);
    DataTable dt = dset.Tables[0];
    GridView1.DataSource = dt;
    GridView1.DataBind();
    for (int i = 1; i < dt.Rows.Count; i++)
    {
    for (int j = 0; j < dt.Columns.Count; j++)
    {
    string Text = dt.Rows[i][j].ToString();
    if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
    {

    }
    else
    {
    if (j == 0)
    {
    msg += "Row Number" + i + " " + " " + "Name Field Missing" + "<br>";
    }
    else if (j == 2)
    {
    msg += "Row Number" + i + " " + " " + "DOB Field Missing" + "<br>";
    }
    else if (j == 4)
    {
    msg += "Row Number" + i + " " + " " + "EMAIL ID Field Missing" + "<br>";
    }
    else if (j == 7)
    {
    msg += "Row Number" + i + " " + " " + "EMPLOYEE ID Field Missing" + "<br>";
    }
    else if (j == 8)
    {
    msg += "Row Number" + i + " " + " " + "BATCH NO Field Missing" + "<br>";
    }
    }
    }
    }

    Session["Message"] = msg;
    }
    catch (Exception ex)
    {
    UploadStatusLabel.Text = "Only .ODS Files Are Allowed";
    }
    GridView1.Visible = true;

    }
    string SaveToAppFolder(HttpPostedFile file)
    {
    string savePath = Server.MapPath(".") + "\\TempFiles\\";
    string fileName = FileUpload1.FileName;
    string pathToCheck = savePath + fileName;
    string tempfileName = "";
    if (System.IO.File.Exists(pathToCheck))
    {
    int counter = 2;
    while (System.IO.File.Exists(pathToCheck))
    {
    tempfileName = counter.ToString() + fileName;
    pathToCheck = savePath + tempfileName;
    counter++;
    }

    fileName = tempfileName;
    }
    else
    {

    }
    savePath += fileName;
    FileUpload1.SaveAs(savePath);
    return savePath;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    GridView1.Visible = false;
    }

    protected void btn_insert_Click(object sender, EventArgs e)
    {

    string msg1 = (string)Session["Message"];
    foreach (GridViewRow g1 in GridView1.Rows)
    {
    SqlConnection con = new SqlConnection(connStr);
    cmd = new SqlCommand("insert into UserDetails1(Name,Address,Dob,Phone,EmailId,LoginName,Password,EmployeeId,BatchNo,Active) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "')", con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    }
    UploadStatusLabel.Text = "Records Inserted Successfully";

    if (string.IsNullOrEmpty(msg1))
    {
    }
    else
    {
    UploadStatusLabel.Text = msg1;
    }
    }

    protected void BtnSave_Click(object sender, EventArgs e)
    {
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=Details.ods");
    Response.ContentType = "vnd.oasis.opendocument.text";
    System.IO.StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();


    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    }
  • #767397
    Hi,

    What did you mean by message to download, as per your content data is download but you are expecting message I didn't get that could you please elaborate with sample.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767399
    which message you want to show ? the code you have written is well and I think it should works well too.
    Please elaborate your issue, so that we can help you more In order to resolve issue

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767403
    Hi,
    After writing grid data to a file, you can add the messages to the file like this:

    protected void BtnSave_Click(object sender, EventArgs e)
    {
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=Details.ods");
    Response.ContentType = "vnd.oasis.opendocument.text";
    System.IO.StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    //... Code to write messages ...
    StringBuilder objSb = new StringBuilder();
    objSb.Append("Messages");
    objSb.Append("\r\n");
    string szText = objSb.ToString();
    foreach (string szMessage in YourMessages)
    {
    szText = szText + szMessage + "\r\n";
    }
    Response.Write(szText);
    //...
    Response.End();
    }

  • #767426
    i had posted my complete code ahta happens is the data in the spread sheet is read and displayed in gridview when we click on insert button the data in the spreadsheet gets inserted into database and if any of the cells are empty in the sheet we will get the label messages the these are empty after insertion.now what i need is when i click on save button the complete data that is spread sheet and the messages should get downloaded but with my save button code only the data in the sheet gets downloaded but not messages can anyone help me out

  • #767431
    Do you mean the error message which says "Cells are empty" also should be downloaded along with the excel!!!!

    I am still confused. Please explain in simple words step by step...


    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM

  • #767474
    yes they also should get downloaded along with excel sheet currently with the above code they are just getting displayed but not getting downloaded how can i download those error messages also can anyone please help me out as i am new to this


Sign In to post your comments