匯出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("匯出攬儲余額表明細" + 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.RegisterStartupScript("
uj5u.com熱心網友回復:
路徑拼錯了吧,"/"改"\\"uj5u.com熱心網友回復:
string fileURL = this.Server.MapPath("./dcexcel/匯出攬儲余額表明細" + jghm+"-"+sj + ".xlsx");是這個路徑嗎?
uj5u.com熱心網友回復:
修改成:string fileURL = this.Server.MapPath("\\dcexcel/匯出攬儲余額表明細" + jghm+"-"+sj + ".xlsx");
這樣對嗎?請大神指點
uj5u.com熱心網友回復:
修改成:string fileURL = this.Server.MapPath("\\dcexcel/匯出攬儲余額表明細" + jghm+"-"+sj + ".xlsx");
測驗了,還是一樣的錯誤。
uj5u.com熱心網友回復:
string filepath = this.Server.MapPath("") + "/dcexcel/";改成
string filepath = this.Server.MapPath("") + "\\dcexcel\\";
看一下C#的轉義字符
uj5u.com熱心網友回復:
服務器上跑excel.interop?這個是要啟動excel的實體的
最好換成其他操作excel的庫
uj5u.com熱心網友回復:
需要換excel的庫uj5u.com熱心網友回復:
樓上兩位大哥,怎么換excel的庫?教哈我uj5u.com熱心網友回復:
再頂,求解。。。。。。轉載請註明出處,本文鏈接:https://www.uj5u.com/net/76333.html
標籤:ASP.NET
上一篇:WPF Path填充不完全
