当我从visual studio运行应用程序时,使用代码生成excel文件。但是当我发布asp.net web应用程序时,它不会显示excel文件。
我做过的事情: 1)添加了对以下内容的引用:1)Interop.excel.dll2)Interop.Microsoft.Office.Core.dll和Interop.Microsoft.Office.Interop.Excel
2)将所有dll的属性更改为Copy local = true。然后将dll添加到bin文件夹中。
3)我通过附加w3wp进程调试了应用程序但代码执行正常,没有异常或错误但是没有生成excel 以下是生成Excel的代码:
public class ExcelUtlity
{
/// <summary>
/// FUNCTION FOR EXPORT TO EXCEL
/// </summary>
/// <param name="dataTable"></param>
/// <param name="worksheetName"></param>
/// <param name="saveAsLocation"></param>
/// <returns></returns>
public bool WriteDataTableToExcel(System.Data.DataTable dataTable, string worksheetName, string saveAsLocation, string ReporType, string fromdate, string todate, string pubicationname)
{
Excel.Application excel;
Excel.Workbook excelworkBook;
Excel.Worksheet excelSheet;
Excel.Range excelCellrange = null;
try
{
// Start Excel and get Application object.
excel = new Excel.Application();
// for making Excel visible
excel.Visible = true;
excel.DisplayAlerts = false;
// Creation a new Workbook
excelworkBook = excel.Workbooks.Add(Type.Missing);
// Workk sheet
excelSheet = (Excel.Worksheet)excelworkBook.Sheets[1];
excelSheet.Name = worksheetName;
excelSheet.Cells[1, 1] = ReporType;
excelCellrange = excelSheet.get_Range("A1", "C1");
excelCellrange.Merge(3);
if (pubicationname != "")
{
excelSheet.Cells[2, 1] = "PublicationName :- " + pubicationname;
excelCellrange = excelSheet.get_Range("A2", "C2");
excelCellrange.Merge(3);
}
else
{
}
excelSheet.Cells[3, 1] = "From Date :- " + fromdate + " To Date :- " + todate;
excelCellrange = excelSheet.get_Range("A3", "C4");
excelCellrange.Merge(3);
// excelSheet.Cells[1, 2] = "Date : " + DateTime.Now.ToShortDateString();
// loop through each row and add values to our sheet
int rowcount = 6;
foreach (DataRow datarow in dataTable.Rows)
{
rowcount += 1;
for (int i = 1; i <= dataTable.Columns.Count; i++)
{
// on the first iteration we add the column headers
if (rowcount == 7)
{
excelSheet.Cells[6, i] = dataTable.Columns[i - 1].ColumnName;
excelSheet.Cells.Font.Color = System.Drawing.Color.Black;
}
excelSheet.Cells[rowcount, i] = datarow[i - 1].ToString();
//for alternate rows
if (rowcount > 7)
{
if (i == dataTable.Columns.Count)
{
if (rowcount % 2 == 0)
{
excelCellrange = excelSheet.Range[excelSheet.Cells[rowcount, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];
// FormattingExcelCells(excelCellrange, "#CCCCFF", System.Drawing.Color.Black, false);
}
}
}
}
}
// now we resize the columns
excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];
excelCellrange.EntireColumn.AutoFit();
Excel.Borders border = excelCellrange.Borders;
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border.Weight = 2d;
excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[2, dataTable.Columns.Count]];
// FormattingExcelCells(excelCellrange, "#000099", System.Drawing.Color.White, true);
//now save the workbook and exit Excel
// excelworkBook.SaveAs(saveAsLocation);
//excelworkBook.Close();
//excel.Quit();
return true;
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
return false;
}
finally
{
excelSheet = null;
excelCellrange = null;
excelworkBook = null;
}
}
/// <summary>
/// FUNCTION FOR FORMATTING EXCEL CELLS
/// </summary>
/// <param name="range"></param>
/// <param name="HTMLcolorCode"></param>
/// <param name="fontColor"></param>
/// <param name="IsFontbool"></param>
public void FormattingExcelCells(Excel.Range range, string HTMLcolorCode, System.Drawing.Color fontColor, bool IsFontbool)
{
range.Interior.Color = System.Drawing.ColorTranslator.FromHtml(HTMLcolorCode);
range.Font.Color = System.Drawing.ColorTranslator.ToOle(fontColor);
if (IsFontbool == true)
{
range.Font.Bold = IsFontbool;
}
}
}
请指导我,我可以做些什么改变,以便它可以在iis上工作。