Good day,
I have a module that upload a record using excel file, and that excel file will be save into the table.. actually i already created that but one ofmy requirement is to clear first the record in excel before it will be uploaded.. how could i clear the sheet of an excel file using C#.
here my code..
string conn = "";
if (FileUpload1.HasFile)
{
string fname = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fLocation = Server.MapPath("/ExcelFileUpload/" + fname);
FileUpload1.SaveAs(fLocation);
if (fExtension == ".xls")
{
conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fExtension == ".xlsx")
{
conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(conn);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtCOO9Report = new DataTable();
con.Open();
DataTable dtExcelsheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelsheetName.Rows[0]["TABLE_NAME"].ToString();
cmd.CommandText = "Select * from [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
DataSet ds = new DataSet("dsExcelC009ReportRecords");
dAdapter.Fill(ds, "dtExcelC009ReportRecords");
foreach (DataColumn dc in ds.Tables["dtExcelC009ReportRecords"].Columns)
{
dc.ColumnMapping = MappingType.Attribute;
}
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
_c009ReportEntities.xmlC009Report = ds.GetXml();
if (_c009ReportBll.InsertXMLC009Report(_c009ReportEntities) > 0)
{
}
for uploading excel file..
once the excel file upload it will converted into xml then that xml will be inserted in the table..
any suggestion on how could i clear my excel file before uploading
thanks