I did an export routine for excel
, using StreamWriter
, but it gives me a problem. I can not get the name of the columns that come in the select. For me to do the header, I had to put it in the hand. This would not be the problem, the issue is when opening the file, it gives a message that the file is not compatible, but then it opens normally. Does anyone have a different path? Below is the code that generates excel, based on a select in Oracle
.
protected void btnExport_Click(object sender, EventArgs e)
{
string caminho = @"C:\Teste_Xls\ListaCliente.xlsx";
string error = string.Empty;
DataExport dataExport = new DataExport();
var connection = ConfigurationManager.ConnectionStrings["DTOP"].ConnectionString;
OracleConnection conn = new OracleConnection(connection);
using (StreamWriter sw = File.CreateText(caminho))
{
using (var connectionOracle = new OracleConnection(connection))
{
try
{
var cmd = new OracleCommand(query, conn);
conn.Open();
using (OracleDataReader reader = cmd.ExecuteReader())
{
sw.WriteLine("Data Inclusao" + "\t" + "Tipo de Tabela" + "\t" + "Tabela" + "\t" + "Codigo" + "\t" + "TUSS");
while (reader.Read())
{
DateTime dtInclusao = Convert.ToDateTime(reader[0]);
string tipoTabela = reader[1].ToString();
string tabela = reader[2].ToString();
Int64 codigo = Convert.ToInt64(reader[3]);
string tuss = reader[4].ToString();
sw.WriteLine(dtInclusao + "\t" + tipoTabela + "\t" + tabela + "\t" + codigo + "\t" + tuss);
}
}
}
catch(Exception ex)
{
error = ex.Message;
}
}
}
//CreateExcelFile.CreateExcelDocument(listaDataExport, @"C:\Teste_Xls\ListaCliente.xlsx");
}