c# 导出excel的时候出现这个问题,服务器重启之后就好了,但是过一段时间又会出现这个问题 服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: 服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT)) 源错误: 行 81: xBk = excel.Workbooks.Add(true);程序源文件
private static string DataView2ExcelBySheet(DataView dv, string fileName)
{
int sheetRows = 65535;//设置Sheet的行数,此为最大上限,本来是65536,因表头要占去一行
int sheetCount = (dv.Table.Rows.Count - 1) / sheetRows + 1;//计算Sheet数 GC.Collect();//垃圾回收 Application excel;
_Workbook xBk;
_Worksheet xSt = null;
excel = new ApplicationClass(); xBk = excel.Workbooks.Add(true); //定义循环中要使用的变量
int dvRowStart;
int dvRowEnd;
int rowIndex = 0;
int colIndex = 0;
//对全部Sheet进行操作
for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
{
//初始化Sheet中的变量
rowIndex = 1;
colIndex = 1;
//计算起始行
dvRowStart = sheetIndex * sheetRows;
dvRowEnd = dvRowStart + sheetRows - 1;
if (dvRowEnd > dv.Table.Rows.Count - 1)
{
dvRowEnd = dv.Table.Rows.Count - 1;
}
//创建一个Sheet
if (null == xSt)
{
xSt = (_Worksheet)xBk.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
}
else
{
xSt = (_Worksheet)xBk.Worksheets.Add(Type.Missing, xSt, 1, Type.Missing);
}
//设置Sheet的名称
xSt.Name = "Expdata";
if (sheetCount > 1)
{
xSt.Name += ((int)(sheetIndex + 1)).ToString();
}
//取得标题
foreach (DataColumn col in dv.Table.Columns)
{
//设置标题格式
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter; //设置标题居中对齐
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;//设置标题为粗体
//填值,并进行下一列
excel.Cells[rowIndex, colIndex++] = col.ColumnName;
}
//取得表格中数量
int drvIndex;
for (drvIndex = dvRowStart; drvIndex <= dvRowEnd; drvIndex++)
{
DataRowView row = dv[drvIndex];
//新起一行,当前单元格移至行首
rowIndex++;
colIndex = 1;
foreach (DataColumn col in dv.Table.Columns)
{
if (col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
}
else if (col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
colIndex++;
}
}
//使用最佳宽度
Range allDataWithTitleRange = xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowIndex, colIndex - 1]);
allDataWithTitleRange.Select();
allDataWithTitleRange.Columns.AutoFit();
allDataWithTitleRange.Borders.LineStyle = 1;//将导出Excel加上边框
}
//设置导出文件在服务器上的文件夹
//string exportDir = "~/ExcelFile/";//注意:该文件夹您须事先在服务器上建好才行
string exportDir = "~/sjgl/MonthData/excel/";//注意:该文件夹您须事先在服务器上建好才行
//设置文件在服务器上的路径
string absFileName = HttpContext.Current.Server.MapPath(System.IO.Path.Combine(exportDir, fileName));
xBk.SaveCopyAs(absFileName);
xBk.Close(false, null, null);
excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xBk = null;
excel = null;
xSt = null;
GC.Collect();
//返回写入服务器Excel文件的路径
return absFileName;
}
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: 服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT)) 源错误: 行 81: xBk = excel.Workbooks.Add(true);程序源文件
private static string DataView2ExcelBySheet(DataView dv, string fileName)
{
int sheetRows = 65535;//设置Sheet的行数,此为最大上限,本来是65536,因表头要占去一行
int sheetCount = (dv.Table.Rows.Count - 1) / sheetRows + 1;//计算Sheet数 GC.Collect();//垃圾回收 Application excel;
_Workbook xBk;
_Worksheet xSt = null;
excel = new ApplicationClass(); xBk = excel.Workbooks.Add(true); //定义循环中要使用的变量
int dvRowStart;
int dvRowEnd;
int rowIndex = 0;
int colIndex = 0;
//对全部Sheet进行操作
for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
{
//初始化Sheet中的变量
rowIndex = 1;
colIndex = 1;
//计算起始行
dvRowStart = sheetIndex * sheetRows;
dvRowEnd = dvRowStart + sheetRows - 1;
if (dvRowEnd > dv.Table.Rows.Count - 1)
{
dvRowEnd = dv.Table.Rows.Count - 1;
}
//创建一个Sheet
if (null == xSt)
{
xSt = (_Worksheet)xBk.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
}
else
{
xSt = (_Worksheet)xBk.Worksheets.Add(Type.Missing, xSt, 1, Type.Missing);
}
//设置Sheet的名称
xSt.Name = "Expdata";
if (sheetCount > 1)
{
xSt.Name += ((int)(sheetIndex + 1)).ToString();
}
//取得标题
foreach (DataColumn col in dv.Table.Columns)
{
//设置标题格式
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter; //设置标题居中对齐
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;//设置标题为粗体
//填值,并进行下一列
excel.Cells[rowIndex, colIndex++] = col.ColumnName;
}
//取得表格中数量
int drvIndex;
for (drvIndex = dvRowStart; drvIndex <= dvRowEnd; drvIndex++)
{
DataRowView row = dv[drvIndex];
//新起一行,当前单元格移至行首
rowIndex++;
colIndex = 1;
foreach (DataColumn col in dv.Table.Columns)
{
if (col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
}
else if (col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
colIndex++;
}
}
//使用最佳宽度
Range allDataWithTitleRange = xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowIndex, colIndex - 1]);
allDataWithTitleRange.Select();
allDataWithTitleRange.Columns.AutoFit();
allDataWithTitleRange.Borders.LineStyle = 1;//将导出Excel加上边框
}
//设置导出文件在服务器上的文件夹
//string exportDir = "~/ExcelFile/";//注意:该文件夹您须事先在服务器上建好才行
string exportDir = "~/sjgl/MonthData/excel/";//注意:该文件夹您须事先在服务器上建好才行
//设置文件在服务器上的路径
string absFileName = HttpContext.Current.Server.MapPath(System.IO.Path.Combine(exportDir, fileName));
xBk.SaveCopyAs(absFileName);
xBk.Close(false, null, null);
excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xBk = null;
excel = null;
xSt = null;
GC.Collect();
//返回写入服务器Excel文件的路径
return absFileName;
}
解决方案 »
- 指定播放器播放指定路径的视频,但是问题出来了
- 求救:如何分配開發任務和進度管理
- 在TextBox 中输入的什么格式,添加数据库后前台显示时也按原来格式显示 怎么实现啊?
- 一个关于JS的问题!!!!
- 頁面自動刷新問題,請大伙幫忙解決!!!
- 小妹提问:如何控制Web窗体大小!
- 求教:关于页面中Session的清除方法....
- 帮助!datagrid内容自动汇入到excel文件,该文件能不能直接放到局域网内的其它计算机上,前提是web服务器已经和其它计算机连接通了。
- datagrid中显示图片的问题
- 在页面的生存期内,自定义组合控件的CreateChildControls方法为何会被两次调入?
- RowCommand问题
- 【VS 2005插件开发】【解决方案+技术】【纯技术问题】
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
string query = "SELECT * FROM jobs";
SqlConnection conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");
SqlDataAdapter da = new SqlDataAdapter(query, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind(); } protected void Button2_Click(object sender, EventArgs e)
{
Response.Clear(); Response.AddHeader("content-disposition",
"attachment;filename=" + HttpUtility.UrlEncode("工作报表.xls", Encoding.UTF8).ToString() + ""); Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.AllowPaging = false;
BindData();
GridView1.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End(); GridView1.AllowPaging = true;
BindData();
} protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData(); }
public override void VerifyRenderingInServerForm(Control control)
{
}
试试这个.最简单的.你拖一个GV
试试看,可能是权限问题