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)
{
}
}