Good I would like that I can be doing an application for which has the functionality to download a file in File Format of values separated by commas Microsoft Excel (.csv) already defined the columns in that file once you download the user with that already template you have to write and save.
The problem is that when I downloaded the excel. this comes out as a preview that is on the right
But when I enter the same file it appears fine. Now that file I enter it and I show it in my gridview
Of course, something strange happens in the preview in the gridview, the same thing also happens
I'm using the dll ClosedXML.Excel;
The code I'm doing is the following to create the csv file
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Contacts");
ws.Cell("A1").Value = "Fecha_Nacimiento";
ws.Cell("B1").Value = "Número";
ws.Cell("C1").Value = "Familia";
ws.Cell("D1").Value = "Dni";
ws.Cell("E1").Value = "Ruc";
ws.Cell("F1").Value = "Apellido_Paterno";
ws.Cell("G1").Value = "Apellido_Materno";
ws.Cell("H1").Value = "Nombres";
ws.Cell("I1").Value = "Celular";
ws.Cell("J1").Value = "Correo";
ws.Cell("K1").Value = "Dirección";
Dtset.Tables[0].TableName = "Persona";
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Reporte.csv");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
Code to show the grid.
string csvPath = Server.MapPath("~/MyFolder/") + Path.GetFileName(inputGroupFile01.PostedFile.FileName);
inputGroupFile01.SaveAs(csvPath);
Label1.Text = inputGroupFile01.FileName + "\'s Información Cargada del excel.";
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new DataColumn[11] {
new DataColumn("Fecha_Nacimiento", typeof(string)),
new DataColumn("Número", typeof(string)),
new DataColumn("Familia", typeof(string)),
new DataColumn("Dni", typeof(string)),
new DataColumn("Ruc",typeof(string)),
new DataColumn("Apellido_Paterno",typeof(string)),
new DataColumn("Apellido_Materno",typeof(string)),
new DataColumn("Nombres",typeof(string)),
new DataColumn("Celular",typeof(string)),
new DataColumn("Correo",typeof(string)),
new DataColumn("Dirección",typeof(string))
});
string csvData = File.ReadAllText(csvPath, Encoding.GetEncoding("iso-8859-1"));
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
int i = 0;
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = HttpUtility.HtmlDecode(cell);
i++;
}
}
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
try
{
var results = from row in ds.Tables["Table1"].AsEnumerable() where row.Field<string>("NOMBRES") == "NOMBRE" select row;
foreach (DataRow row in results)
{
ds.Tables["Table1"].Rows.Remove(row);
}
}
catch { }
GridView1.DataSource = dt;
GridView1.DataBind();