Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp net项目//导出Excel功能失败_C#_Sql_Asp.net_Excel - Fatal编程技术网

C# Asp net项目//导出Excel功能失败

C# Asp net项目//导出Excel功能失败,c#,sql,asp.net,excel,C#,Sql,Asp.net,Excel,您好,我的学徒期已经结束,我必须自己创建一个项目。我正在开发某种网站,我想添加一个函数,将sql信息下载到excel文件中。在我本地的VisualStudio版本上,它工作得非常好,但当我尝试使用IIS在我的Web服务器上运行时,它会崩溃,并出现一个错误,这对我有限的知识来说至少是没有帮助的。希望这里的人能帮助解决这个问题 Export Code: using System; using System.Net; using System.IO; using System.Web.UI; usin

您好,我的学徒期已经结束,我必须自己创建一个项目。我正在开发某种网站,我想添加一个函数,将sql信息下载到excel文件中。在我本地的VisualStudio版本上,它工作得非常好,但当我尝试使用IIS在我的Web服务器上运行时,它会崩溃,并出现一个错误,这对我有限的知识来说至少是没有帮助的。希望这里的人能帮助解决这个问题

Export Code:
using System;
using System.Net;
using System.IO;
using System.Web.UI;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections.Generic;

if (download_decision == "Partial")
            {
                //New Excel Application
                Excel.Application XL = new Microsoft.Office.Interop.Excel.Application();

                //Hiding Alerts and the File
                XL.Visible = false;
                XL.DisplayAlerts = false;

                //Workbook opened
                Excel.Workbook WB = XL.Workbooks.Add();

                //All Sheets are selected
                Excel.Sheets sheets = WB.Worksheets;

                //First Worksheets selected and renamed
                Excel.Worksheet WS = sheets.get_Item(1);
                WS.Name = public_language;

                //Name columns
                (WS.Cells[1, 1] as Excel.Range).Value = public_language;
                (WS.Cells[1, 2] as Excel.Range).Value = "English";

                (WS.Cells[1, 1] as Excel.Range).Font.Bold = true;
                (WS.Cells[1, 2] as Excel.Range).Font.Bold = true;

                (WS.Cells[1, 1] as Excel.Range).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                (WS.Cells[1, 2] as Excel.Range).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;

                //SQL Reader
                using (SqlConnection conn = new SqlConnection())
                {
                    //SQL Server
                    conn.ConnectionString = sqlserver;

                    //Conncection establish
                    conn.Open();

                    //Get SQL Information
                    SqlCommand cmd = new SqlCommand("select * from" + " " + selected_table, conn);
                    SqlDataReader rdr = cmd.ExecuteReader();

                    int counter = 3;

                    while (rdr.Read())
                    {
                        string Source = (string)rdr["source"];
                        string Target = (string)rdr["target"];

                        //Values are written in file
                        (WS.Cells[counter, 1] as Excel.Range).Value = Source;
                        (WS.Cells[counter, 2] as Excel.Range).Value = Target;

                        counter++;
                    }
                }
                //Autofit Cells
                Excel.Range workSheet_range = WS.get_Range("A:B");
                workSheet_range.EntireColumn.AutoFit();

                //Save and Close
                WB.SaveAs(path);
                WB.Close();

            }
错误向我显示:

Server Error in '/' Application.
Exception from HRESULT: 0x800AC472
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800AC472

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[COMException (0x800ac472): Exception from HRESULT: 0x800AC472]
   System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) +1398
   Microsoft.Office.Interop.Excel.Font.set_Bold(Object ) +0
   Glossary.Dashboard.Download_Click(Object sender, EventArgs e) in C:\Users\myuser\source\repos\project1\project1\Dashboard.aspx.cs:107
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +11773973
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +150
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5062

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.2106.0

多谢各位

@磁控管非常感谢您!你的想法很完美。我现在正在使用ePlus。我修改了我的代码,作为回报,它工作了,速度大大提高

我的新代码和工作解决方案:

using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Collections.Generic;

if (download_decision == "Partial")
            {
                //New Excel Application
                ExcelPackage excel = new ExcelPackage();
                var ws = excel.Workbook.Worksheets.Add(public_language);

                //Name columns
                using (ExcelRange Rng = ws.Cells[1, 1])
                {
                    Rng.Value = public_language;
                    Rng.Style.Font.Bold = true;
                    Rng.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
                }

                //Name columns
                using (ExcelRange Rng = ws.Cells[1, 2])
                {
                    Rng.Value = "English";
                    Rng.Style.Font.Bold = true;
                    Rng.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
                }

                //SQL Reader
                using (SqlConnection conn = new SqlConnection())
                {
                    //SQL Server
                    conn.ConnectionString = sqlserver;

                    //Conncection establish
                    conn.Open();

                    //Get SQL Information
                    SqlCommand cmd = new SqlCommand("select * from" + " " + selected_table, conn);
                    SqlDataReader rdr = cmd.ExecuteReader();

                    int counter = 3;

                    while (rdr.Read())
                    {
                        string Source = (string)rdr["source"];
                        string Target = (string)rdr["target"];

                        //Values are written in file
                        ws.Cells[counter, 1].Value = Source;
                        ws.Cells[counter, 2].Value = Target;

                        counter++;
                    }
                }

                //Autofit
                ws.Column(1).AutoFit();
                ws.Column(2).AutoFit();

                //Save and Close
                Stream stream = File.Create(path);
                excel.SaveAs(stream);
                stream.Close();
            }

//transfer to client
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                response.Clear();
                response.ClearHeaders();
                response.ClearContent();
                response.AddHeader("content-disposition", "attachment; filename=" + filename);
                response.AddHeader("content-type", "application/excel");
                response.ContentType = "application/vnd.xls";
                response.AddHeader("content-length", file.Length.ToString());
                response.WriteFile(file.FullName);
                response.Flush();
                response.Close();
            }

            //file deleting
            File.Delete(path);

您使用的是Interop,这意味着运行代码的计算机必须安装MS Excel,而服务器通常不安装。您的IIS服务器是否安装了MS Excel?否则,您将不得不更改整个代码以使用另一个库来读取Excel。您好,是的,我在服务器上安装了Excel,但为了确保已卸载它,我将尝试在明天重新安装它。如果错误仍然存在,我将尝试解决它,但我尝试了一整天,这是恼人的。谢谢:)如果可能的话,我建议您放弃互操作,使用另一个不是在Excel或COM上开发的库,如ePlus。如果这都是你的代码,你应该不会有太多的麻烦。