各位高手:
我现在遇到一个非常棘手的问题:
我是用C#.NET开发的网站应用程序,要导出EXCEL,输出EXCEL的功能已经实现.
我在我自己的机器IIS上发布程序,运行一切正常.但是我一把程序发布到外网服务器上时,怎么输出不了EXCEL呢?
让我非常郁闷,我自己机器上输出路径里能找到EXCEL文件,可是在外网服务上发布的输出路径里什么也没有.
请各位高手救救我啊,高分酬谢!
附源代码:
=======下面是导EXCEL函数======
public static bool ExportToExcel(System.Data.DataTable dt, string AbosultedFilePath)
{
//检查数据表是否为空,如果为空,则退出
if (dt == null)
return false;
Excel.Application xlApp = new Excel.Application();
object objOpt = System.Reflection.Missing.Value;
//创建Excel应用程序对象,如果未创建成功则退出
if (xlApp == null)
{
MagicAjax.AjaxCallHelper.WriteAddScriptElementScript("alert('无法创建Excel对象,可能你的电脑未装Excel!')", new System.Collections.Specialized.NameValueCollection());
return false;
} Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1]; //取得sheet1
Excel.Range range = null;
long totalCount = dt.Rows.Count;
long rowRead = 0;
float percent = 0; //写入标题
for (int i = 0; i < dt.Columns.Count; i++)
{
//写入标题名称
worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; //设置标题的样式
range = (Excel.Range)worksheet.Cells[1, i + 1];
range.Font.Bold = true; //粗体
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //居中 range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); //背景色
range.EntireColumn.AutoFit(); //自动设置列宽
range.EntireRow.AutoFit(); //自动设置行高
} //写入DataTable中数据的内容
for (int r = 0; r < dt.Rows.Count; r++)
{
for (int c = 0; c < dt.Columns.Count; c++)
{
//写入内容
worksheet.Cells[r + 2, c + 1] = "'" + dt.Rows[r][c].ToString();
//设置样式
range = (Excel.Range)worksheet.Cells[r + 2, c + 1];
range.Font.Size = 9; //字体大小
range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); //加边框
range.EntireColumn.AutoFit(); //自动调整列宽
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
System.Windows.Forms.Application.DoEvents();
} range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
if (dt.Columns.Count > 1)
{
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
} try
{
workbook.Saved = true;
workbook.SaveCopyAs(AbosultedFilePath);
//workbook.SaveAs(string.Format("{0}/{1}", System.Windows.Forms.Application.StartupPath, filename), objOpt, null, null, false, false, XlSaveAsAccessMode.xlNoChange, ConflictOption.OverwriteChanges, null, null, null, null);
}
catch (Exception ex)
{
MagicAjax.AjaxCallHelper.WriteAddScriptElementScript("alert('导出文件时出错,文件可能正被打开!')", new System.Collections.Specialized.NameValueCollection());
//System.Web.HttpContext.Current.Response.Write("导出文件时出错,文件可能正被打开!\n" + ex.ToString());
return false;
}
//workbook.SaveAs();
workbooks.Close(); if (xlApp != null)
{
xlApp.Workbooks.Close();
xlApp.Quit(); int generation = System.GC.GetGeneration(xlApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null;
System.GC.Collect(generation);
}
GC.Collect(); //强行销毁 #region 强行杀死最近打开的Excel进程
System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
System.DateTime startTime = new DateTime();
int m, killID = 0;
for (m = 0; m < excelProc.Length; m++)
{
if (startTime < excelProc[m].StartTime)
{
startTime = excelProc[m].StartTime;
killID = m;
}
}
if (excelProc[killID].HasExited == false)
{
excelProc[killID].Kill();
}
#endregion return true;
}
===========下面是函数调用=============================
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = Apply.ExportExcel(DRP_ND.SelectedValue);
string urlPath = HttpContext.Current.Request.ApplicationPath + "/excel/";
string physicPath = HttpContext.Current.Server.MapPath(urlPath);
string FileName = physicPath + "test.xls"; try
{
if (ExportToExcel(dt,FileName))
{
MagicAjax.AjaxCallHelper.Redirect("excel/" + filename + ".xls");
}
}
catch
{
MagicAjax.AjaxCallHelper.Write("window.open('excel/" + filename + ".xls');");
}
}
我现在遇到一个非常棘手的问题:
我是用C#.NET开发的网站应用程序,要导出EXCEL,输出EXCEL的功能已经实现.
我在我自己的机器IIS上发布程序,运行一切正常.但是我一把程序发布到外网服务器上时,怎么输出不了EXCEL呢?
让我非常郁闷,我自己机器上输出路径里能找到EXCEL文件,可是在外网服务上发布的输出路径里什么也没有.
请各位高手救救我啊,高分酬谢!
附源代码:
=======下面是导EXCEL函数======
public static bool ExportToExcel(System.Data.DataTable dt, string AbosultedFilePath)
{
//检查数据表是否为空,如果为空,则退出
if (dt == null)
return false;
Excel.Application xlApp = new Excel.Application();
object objOpt = System.Reflection.Missing.Value;
//创建Excel应用程序对象,如果未创建成功则退出
if (xlApp == null)
{
MagicAjax.AjaxCallHelper.WriteAddScriptElementScript("alert('无法创建Excel对象,可能你的电脑未装Excel!')", new System.Collections.Specialized.NameValueCollection());
return false;
} Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1]; //取得sheet1
Excel.Range range = null;
long totalCount = dt.Rows.Count;
long rowRead = 0;
float percent = 0; //写入标题
for (int i = 0; i < dt.Columns.Count; i++)
{
//写入标题名称
worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; //设置标题的样式
range = (Excel.Range)worksheet.Cells[1, i + 1];
range.Font.Bold = true; //粗体
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //居中 range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); //背景色
range.EntireColumn.AutoFit(); //自动设置列宽
range.EntireRow.AutoFit(); //自动设置行高
} //写入DataTable中数据的内容
for (int r = 0; r < dt.Rows.Count; r++)
{
for (int c = 0; c < dt.Columns.Count; c++)
{
//写入内容
worksheet.Cells[r + 2, c + 1] = "'" + dt.Rows[r][c].ToString();
//设置样式
range = (Excel.Range)worksheet.Cells[r + 2, c + 1];
range.Font.Size = 9; //字体大小
range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); //加边框
range.EntireColumn.AutoFit(); //自动调整列宽
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
System.Windows.Forms.Application.DoEvents();
} range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
if (dt.Columns.Count > 1)
{
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
} try
{
workbook.Saved = true;
workbook.SaveCopyAs(AbosultedFilePath);
//workbook.SaveAs(string.Format("{0}/{1}", System.Windows.Forms.Application.StartupPath, filename), objOpt, null, null, false, false, XlSaveAsAccessMode.xlNoChange, ConflictOption.OverwriteChanges, null, null, null, null);
}
catch (Exception ex)
{
MagicAjax.AjaxCallHelper.WriteAddScriptElementScript("alert('导出文件时出错,文件可能正被打开!')", new System.Collections.Specialized.NameValueCollection());
//System.Web.HttpContext.Current.Response.Write("导出文件时出错,文件可能正被打开!\n" + ex.ToString());
return false;
}
//workbook.SaveAs();
workbooks.Close(); if (xlApp != null)
{
xlApp.Workbooks.Close();
xlApp.Quit(); int generation = System.GC.GetGeneration(xlApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null;
System.GC.Collect(generation);
}
GC.Collect(); //强行销毁 #region 强行杀死最近打开的Excel进程
System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
System.DateTime startTime = new DateTime();
int m, killID = 0;
for (m = 0; m < excelProc.Length; m++)
{
if (startTime < excelProc[m].StartTime)
{
startTime = excelProc[m].StartTime;
killID = m;
}
}
if (excelProc[killID].HasExited == false)
{
excelProc[killID].Kill();
}
#endregion return true;
}
===========下面是函数调用=============================
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = Apply.ExportExcel(DRP_ND.SelectedValue);
string urlPath = HttpContext.Current.Request.ApplicationPath + "/excel/";
string physicPath = HttpContext.Current.Server.MapPath(urlPath);
string FileName = physicPath + "test.xls"; try
{
if (ExportToExcel(dt,FileName))
{
MagicAjax.AjaxCallHelper.Redirect("excel/" + filename + ".xls");
}
}
catch
{
MagicAjax.AjaxCallHelper.Write("window.open('excel/" + filename + ".xls');");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货