求助:异常来自HRESULT:0x800A03EC

nx_0082 2020-01-13 08:37:12
导出EXCEL提示:“异常来自HRESULT:0x800A03EC”,OFFICE版本为2010,更换为2007之后提示也一样。
错误提示截图以及代码见附件,求教各位大神。

代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.Common;
using System.IO;


public partial class jgncxx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string name = (string)Session["name_pub"]; /*取得自己的会话中的姓名参数*/
if (name == null)
{
Response.Redirect("Default.aspx");
}
}
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
string name = (string)Session["name_pub"]; /*取得自己的会话中的姓名参数*/
Label Mlable1 = (Label)this.Master.FindControl("Label2");/*访问母版页上的标签控件*/
Mlable1.Text = "欢迎:" + name;
string yhjb1 = (string)Session["jb_pub"];/*接收传递过来的用户级别参数*/
//Button Mbut1 = (Button)this.Master.FindControl("Button3");/*访问母版页上的按扭控件*/
//Button Mbut2 = (Button)this.Master.FindControl("Button4");/*访问母版页上的按扭控件*/
/*if (Convert.ToInt32(yhjb1) == 1)
{
Mbut1.Visible = true;//如果是管理员则显示用户管理按扭和数据导入按钮
Mbut2.Visible = true;
}
else
{
Mbut1.Visible = false;
Mbut2.Visible = false;
}
*/
}
//连接到揽储系统数据库
public SqlConnection GetConnection()
{
string stglStr = ConfigurationManager.AppSettings["connectionString"].ToString();
SqlConnection myConn = new SqlConnection(stglStr);
return myConn;
}


//自定义一个数据绑定函数Grid1ViewBind
protected void Grid1ViewBind()
{
SqlConnection stglconn = GetConnection();
stglconn.Open();
//string lcrid = (string)Session["dlmc_pub"];//取得揽储人ID
string jghm = (string)Session["jghm_pub"];////取得揽储人机构号码
string date_str = TextBox1.Text;
string year1 = date_str.Substring(0, 4);
string month1 = date_str.Substring(5, 2);
string day1 = date_str.Substring(8, 2);
string sqlstr_sel = "select * from 揽储余额表明细 where 年份=@year1 and 月份=@month1 and 天数=@day1 and 机构号码=@jghm";
SqlCommand mycmd_sel = new SqlCommand(sqlstr_sel, stglconn);
mycmd_sel.Parameters.Add("@year1", System.Data.SqlDbType.NVarChar, 4).Value = year1;
mycmd_sel.Parameters.Add("@month1", System.Data.SqlDbType.NVarChar, 2).Value = month1;
mycmd_sel.Parameters.Add("@day1", System.Data.SqlDbType.NVarChar, 2).Value = day1.Trim();
mycmd_sel.Parameters.Add("@jghm", System.Data.SqlDbType.NVarChar, 10).Value = jghm.Trim();

SqlDataAdapter myda_sel = new SqlDataAdapter(mycmd_sel);
DataSet myds_sel = new DataSet();
myda_sel.Fill(myds_sel);
//求出已填充表myds_sel中的时占余额的和
string jehj = myds_sel.Tables[0].Compute("sum(时点余额)", "true").ToString();
Label5.Text = jehj;
GridView1.DataSource = myds_sel.Tables[0];
GridView1.DataBind();
stglconn.Dispose();
myda_sel.Dispose();
myds_sel.Dispose();
}

protected void Grid2ViewBind()
{
SqlConnection stglconn = GetConnection();
stglconn.Open();
//string lcrid = (string)Session["dlmc_pub"];//取得揽储人ID
string jghm = (string)Session["jghm_pub"];////取得揽储人机构号码
string date_str = TextBox2.Text;
string year1 = date_str.Substring(0, 4);
string month1 = date_str.Substring(5, 2);
string day1 = date_str.Substring(8, 2);
string sqlstr_sel = "select * from 揽储余额表汇总 where 年份=@year1 and 月份=@month1 and 天数=@day1 and 机构号码=@jghm";
SqlCommand mycmd_sel = new SqlCommand(sqlstr_sel, stglconn);
mycmd_sel.Parameters.Add("@year1", System.Data.SqlDbType.NVarChar, 4).Value = year1;
mycmd_sel.Parameters.Add("@month1", System.Data.SqlDbType.NVarChar, 2).Value = month1;
mycmd_sel.Parameters.Add("@day1", System.Data.SqlDbType.NVarChar, 2).Value = day1.Trim();
mycmd_sel.Parameters.Add("@jghm", System.Data.SqlDbType.NVarChar, 10).Value = jghm.Trim();

SqlDataAdapter myda_sel = new SqlDataAdapter(mycmd_sel);
DataSet myds_sel = new DataSet();
myda_sel.Fill(myds_sel);
GridView2.DataSource = myds_sel.Tables[0];
GridView2.DataBind();
stglconn.Dispose();
myda_sel.Dispose();
myds_sel.Dispose();
}
protected void Button6_Click(object sender, EventArgs e)
{

if (TextBox1.Text == "")
{
this.RegisterStartupScript("", "<script language=javascript>alert('请选择查询时间!')</script>");
return;
}
//调用绑定函数
Grid1ViewBind();

}


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//调用绑定函数
Grid1ViewBind();
}

protected void Button8_Click(object sender, EventArgs e)
{
Response.Redirect("ncxx.aspx");
}



protected void Button10_Click(object sender, EventArgs e)
{
if (TextBox2.Text == "")
{
this.RegisterStartupScript("", "<script language=javascript>alert('请选择查询时间!')</script>");
return;
}
//调用绑定函数
Grid2ViewBind();
}
protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
//调用绑定函数
Grid2ViewBind();
}


protected void Button17_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
this.RegisterStartupScript("", "<script language=javascript>alert('请选择查询时间!')</script>");
return;
}
string jghm = (string)Session["jghm_pub"];//取得揽储人机构号码
string date_str = TextBox1.Text;
string year1 = date_str.Substring(0, 4);
string month1 = date_str.Substring(5, 2);
string day1 = date_str.Substring(8, 2);
SqlConnection stglconn_dc = GetConnection();
stglconn_dc.Open();
string strsel_dc = "select 年份,月份,天数,揽储人ID,揽储人姓名,机构号码,姓名,账号,时点余额 from 揽储余额表明细 where 年份=@year1 and 月份=@month1 and 天数=@day1 and 机构号码=@jghm";

SqlCommand mycmd_sel = new SqlCommand(strsel_dc, stglconn_dc);
mycmd_sel.Parameters.Add("@year1", System.Data.SqlDbType.NVarChar, 4).Value = year1;
mycmd_sel.Parameters.Add("@month1", System.Data.SqlDbType.NVarChar, 2).Value = month1;
mycmd_sel.Parameters.Add("@day1", System.Data.SqlDbType.NVarChar, 2).Value = day1.Trim();
mycmd_sel.Parameters.Add("@jghm", System.Data.SqlDbType.NVarChar, 10).Value = jghm.Trim();
SqlDataAdapter myda_sel = new SqlDataAdapter(mycmd_sel);
DataSet myds_sel = new DataSet();
myda_sel.Fill(myds_sel);


Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Add();
Excel.Worksheet worksheet = workbook.ActiveSheet as Excel.Worksheet;

worksheet.Cells[1, 1] = "年份";
worksheet.Cells[1, 2] = "月份";
worksheet.Cells[1, 3] = "天数";
worksheet.Cells[1, 4] = "揽储人姓名";
worksheet.Cells[1, 5] = "机构号码";
worksheet.Cells[1, 6] = "姓名";
worksheet.Cells[1, 7] = "账号";
worksheet.Cells[1, 8] = "时点余额";
var index = 2;
for (int i = 0; i <= myds_sel.Tables[0].Rows.Count - 1; i++)
{
worksheet.Cells[index, 1] = myds_sel.Tables[0].Rows[i]["年份"].ToString();
worksheet.Cells[index, 2] = myds_sel.Tables[0].Rows[i]["月份"].ToString();
worksheet.Cells[index, 3] = myds_sel.Tables[0].Rows[i]["天数"].ToString();
worksheet.Cells[index, 4] = myds_sel.Tables[0].Rows[i]["揽储人姓名"].ToString();
worksheet.Cells[index, 5] = myds_sel.Tables[0].Rows[i]["机构号码"].ToString();
worksheet.Cells[index, 6] = myds_sel.Tables[0].Rows[i]["姓名"].ToString();
worksheet.Cells[index, 7] = myds_sel.Tables[0].Rows[i]["账号"].ToString() + "\t";//将列设置为文本格式
worksheet.Cells[index, 8] = myds_sel.Tables[0].Rows[i]["时点余额"].ToString();
index++;
}


//string xs = System.DateTime.Now.Hour.ToString();/*取系统时间中的小时*/
//string fz = System.DateTime.Now.Minute.ToString();/*取系统时间中的分钟*/
//string sj = year1 + "-" + month1 + "-" + day1 + "-" + xs + "-" + fz;
string sj = year1 + "-" + month1 + "-" + day1;

string filepath = this.Server.MapPath("") + "/dcexcel/";
string filepath1 = filepath + "导出揽储余额表明细" + jghm + "-" + sj + ".xlsx";
if (File.Exists(filepath1))
{
File.Delete(filepath1);//如果文件已经存在,则先删除该文件

}


workbook.SaveAs(filepath1);
workbook.Close();
application.Quit();
stglconn_dc.Close();
stglconn_dc.Dispose();
mycmd_sel.Dispose();
myds_sel.Dispose();
myda_sel.Dispose();

//将导出的文件下载到客户端硬盘
string fileURL = this.Server.MapPath("./dcexcel/导出揽储余额表明细" + jghm+"-"+sj + ".xlsx");//文件路径,可用相对路径
FileInfo fileInfo = new FileInfo(fileURL);
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(fileInfo.Name.ToString()));//文件名
Response.AddHeader("content-length", fileInfo.Length.ToString());//文件大小
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.WriteFile(fileURL);
//this.RegisterStartupScript("", "<script language=javascript>alert('导出数据成功!谢谢')</script>");
}
protected void Button18_Click(object sender, EventArgs e)
{
if (TextBox2.Text == "")
{
this.Registe
...全文
124 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧