I'm creating an excel file, and I must open it immediately. Obviously, when I create it on localhost, I can get the file, but when I publish my web application on IIS this not works because I suppose It's creating on server side...
So I create the excel file and try to upload to the server and after that download it, but I can't create it, If I run this on localhost works, but when I publish don't work.
Any advice?
this is my code:
I use this libraries for Excel:
using Microsoft.Office.Core;
using Excelo = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop;
using System.Reflection;
using System.Globalization;
Then, I start my excel file:
private void sacConsulPend_GenRemito()
{ // DB
string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
DataSet dsRemito = new DataSet();
DataTable dtCons = new DataTable();
using (SqlConnection sqlCon = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{ //SP for data query
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "SACBUS_CONSUL_SOLICITUDEXIST";
cmd.Parameters.AddWithValue("@ID_CONSULTA",
Convert.ToInt32(lblSac_ConPenDet_idCons.Text.Replace(" ", "")));
cmd.Connection = sqlCon;
sqlCon.Open();
SqlDataAdapter dapC = new SqlDataAdapter(cmd);
dapC.Fill(dsRemito);
}
catch (Exception sqlex)
{
throw sqlex;
}
}
}
Excelo.Application oXL;
Excelo._Workbook oWB;
Excelo._Worksheet oSheet;
Excelo.Range oRng;
try
{
//Start Excel
oXL = new Excelo.Application();
//new blank workbook.
oWB = (Excelo._Workbook)(oXL.Workbooks.Add(System.Reflection.Missing.Value));
oSheet = (Excelo._Worksheet)oWB.ActiveSheet;
object misValue = System.Reflection.Missing.Value;
//Text Format
oSheet.get_Range("A1", "Z1000").Font.Name = "Courier New";
oSheet.get_Range("A1", "Z1000").Font.Size = 7;
oSheet.get_Range("A1", "Z1000").Font.Bold = true;
oSheet.get_Range("A1", "Z1000").NumberFormat = "@";
oSheet.get_Range("D1", "E1").Cells.Merge();
oSheet.get_Range("A1", "F1").Font.Bold = true;
oSheet.get_Range("A1", "F1").Font.Italic = true;
oSheet.get_Range("A1", "F1").VerticalAlignment = Excelo.XlVAlign.xlVAlignCenter;
oSheet.get_Range("A1", "F1").HorizontalAlignment = Excelo.XlVAlign.xlVAlignCenter;
oSheet.get_Range("A1", "F1").Font.Size = 9;
//Logo Lockers
oSheet.Shapes.AddPicture(Server.MapPath("~/images/LockersLogo.jpg"), MsoTriState.msoFalse, MsoTriState.msoCTrue, 0, 0, 60, 20);
/*
Excel Content
*/
//Save ExcelFile
var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Files/Envios/"), lblSac_ConPenDet_idCons.Text.Replace(" ", "") + ".xlsx");
oWB.SaveAs(filePath, Excelo.XlFileFormat.xlOpenXMLWorkbook, Missing.Value,
Missing.Value, false, false, Excelo.XlSaveAsAccessMode.xlNoChange,
Excelo.XlSaveConflictResolution.xlUserResolution, true,
Missing.Value, Missing.Value, Missing.Value);
}
catch (Exception thex)
{
String errorMessage;
errorMessage = "Error en la creacion del archivo Excel: ";
errorMessage = String.Concat(errorMessage, thex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, thex.Source);
}
}