Excel.Application xlAppToExport = new Excel.Application();
excel Export working on local but not working on server.i can't getting any error.
What I have tried:
Excel.Application xlAppToExport = new Excel.Application();
xlAppToExport.Workbooks.Add("");
// ADD A WORKSHEET.
Excel.Worksheet xlWorkSheetToExport = default(Excel.Worksheet);
xlWorkSheetToExport = (Excel.Worksheet)xlAppToExport.Sheets["Sheet1"];
// ROW ID FROM WHERE THE DATA STARTS SHOWING.
int iRowCnt = 4;
// SHOW THE HEADER.
xlWorkSheetToExport.Cells[1, 1] = "GST Information For Customer";
Excel.Range range = xlWorkSheetToExport.Cells[1, 1] as Excel.Range;
range.EntireRow.Font.Name = "Calibri";
range.EntireRow.Font.Bold = true;
range.EntireRow.Font.Size = 20;
//Excel.Range ranget = xlWorkSheetToExport.Cells[1, 4] as Excel.Range;
//ranget.EntireRow.Font.Name = "Calibri";
//ranget.EntireRow.Font.Bold = true;
//ranget.EntireRow.Font.Size = 20;
//range.EntireRow.VerticalAlignment = "Center";
xlAppToExport.get_Range("A1", "A20").Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
xlWorkSheetToExport.Range["A1:B1"].MergeCells = true; // MERGE CELLS OF THE HEADER.:c1:d1:e1:f1:g1:h1:i1:j1:k1:l1:m1:n1:o1:p1
xlAppToExport.get_Range("B1", "B20").Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
Excel.Range chartRange;
chartRange = xlWorkSheetToExport.get_Range("A1", "A20");
chartRange.Font.Bold = true;
// SHOW COLUMNS ON THE TOP.
xlWorkSheetToExport.Cells[4, 1] = "Type";
xlWorkSheetToExport.Cells[5, 1] = "EmailId";
xlWorkSheetToExport.Cells[6, 1] = "Name";
xlWorkSheetToExport.Cells[7, 1] = "PANNo";
xlWorkSheetToExport.Cells[8, 1] = "ProGstNo";
xlWorkSheetToExport.Cells[9, 1] = "State";
xlWorkSheetToExport.Cells[10, 1] = "City";
xlWorkSheetToExport.Cells[11, 1] = "ContactPerson";
xlWorkSheetToExport.Cells[12, 1] = "ContactNumber";
xlWorkSheetToExport.Cells[13, 1] = "Comments";
xlWorkSheetToExport.Cells[14, 1] = "AcHolderName";
xlWorkSheetToExport.Cells[15, 1] = "Bank";
xlWorkSheetToExport.Cells[16, 1] = "Branch";
xlWorkSheetToExport.Cells[17, 1] = "AcNumber";
xlWorkSheetToExport.Cells[18, 1] = "IFSCCode";
xlWorkSheetToExport.Cells[19, 1] = "MICRCode";
//xlWorkSheetToExport.Cells[20, 1] = "GstAcknowCopy";
//xlWorkSheetToExport.Cells[iRowCnt - 1, 3] = "PresentAddress";
//xlWorkSheetToExport.Cells[iRowCnt - 1, 4] = "Email Address";
int i;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
xlWorkSheetToExport.Cells[4, 2] = dt.Rows[i].Field<string>("Type");
xlWorkSheetToExport.Cells[5, 2] = dt.Rows[i].Field<string>("EmailId");
xlWorkSheetToExport.Cells[6, 2] = dt.Rows[i].Field<string>("Name");
xlWorkSheetToExport.Cells[7, 2] = dt.Rows[i].Field<string>("PANNo");
xlWorkSheetToExport.Cells[8, 2] = dt.Rows[i].Field<string>("ProGstNo");
xlWorkSheetToExport.Cells[9, 2] = dt.Rows[i].Field<string>("State");
xlWorkSheetToExport.Cells[10, 2] = dt.Rows[i].Field<string>("City");
xlWorkSheetToExport.Cells[11, 2] = dt.Rows[i].Field<string>("ContactPerson");
xlWorkSheetToExport.Cells[12, 2] = dt.Rows[i].Field<string>("ContactNumber");
xlWorkSheetToExport.Cells[13, 2] = dt.Rows[i].Field<string>("Comments");
xlWorkSheetToExport.Cells[14, 2] = dt.Rows[i].Field<string>("AcHolderName");
xlWorkSheetToExport.Cells[15, 2] = dt.Rows[i].Field<string>("Bank");
xlWorkSheetToExport.Cells[16, 2] = dt.Rows[i].Field<string>("Branch");
xlWorkSheetToExport.Cells[17, 2] = dt.Rows[i].Field<string>("AcNumber");
xlWorkSheetToExport.Cells[18, 2] = dt.Rows[i].Field<string>("IFSCCode");
xlWorkSheetToExport.Cells[19, 2] = dt.Rows[i].Field<string>("MICRCode");
//xlWorkSheetToExport.Cells[19, 2] = dt.Rows[i].Field<string>("GstAcknowCopy");
//xlWorkSheetToExport.Cells[iRowCnt, 3] = dt.Rows[i].Field("PresentAddress");
//xlWorkSheetToExport.Cells[iRowCnt, 4] = dt.Rows[i].Field("Email");
iRowCnt = iRowCnt + 1;
}
// FINALLY, FORMAT THE EXCEL SHEET USING EXCEL'S AUTOFORMAT FUNCTION.
Excel.Range range1 = xlAppToExport.ActiveCell.Worksheet.Cells[1, 1] as Excel.Range;
range1.AutoFormat(ExcelAutoFormat.xlRangeAutoFormatList3);
range1.Rows.AutoFit();
range1.Columns.AutoFit();
xlAppToExport.Workbooks.Close();
xlAppToExport.Quit();
xlAppToExport = null;
xlWorkSheetToExport = null;