【问题标题】:Opening Excel file Using ClosedXML Error使用 ClosedXML 错误打开 Excel 文件
【发布时间】:2016-04-17 19:41:54
【问题描述】:

我正在尝试使用 ClosedXML 在 ASP.NET 中打开一个 6MB 的 Excel 文件,但我收到一条错误消息 “隐式转换出错。无法转换空对象。”

这是我的代码:

    Dim temppath = Path.GetTempPath()
    Dim filenamestr As String = Path.GetFileNameWithoutExtension(Path.GetRandomFileName())

    Dim tempfilename As String = Path.Combine(temppath, filenamestr + ".xlsx")
    Using fs = New FileStream(tempfilename, FileMode.Create, FileAccess.Write)
        xlStream.WriteTo(fs)
    End Using

    Dim xlwb = New XLWorkbook(tempfilename) 'The part having the error

来源:“DocumentFormat.OpenXml” 我也尝试打开一个现有的 Excel 文件,它仍然会导致这个错误。还尝试将文件放在不同的目录中,认为这只是因为我的驱动器的许可,一点运气都没有。提前致谢。

【问题讨论】:

    标签: asp.net vb.net excel closedxml


    【解决方案1】:

    以下是用于在封闭的 xml 中获取 excel 文件的实时工作代码

     private void workbookProcessing(string workbookname)
        {
            SqlDatabase objdb = new SqlDatabase ( OSMC.constring_Property );
    

    //boqserverfilepath下面是excel文件存放的文件夹 EX: "~/Uploaded_Boq/";

            string fullfilename = System.Web.HttpContext.Current.Server.MapPath ( OneStopMethods_Common.boqserverfilepath + workbookname );
            XLWorkbook theWorkBook = new XLWorkbook ( fullfilename );
            int worksheetcount = theWorkBook.Worksheets.Count;
            foreach(IXLWorksheet theWorkSheet in theWorkBook.Worksheets)
            {
                foreach(IXLRow therow in theWorkSheet.Rows())
                {
                    foreach(IXLCell thecell in therow.Cells())
                    {
                                            int tenderid = 1001;
    
                                            int  Activity_Section_objseq = tenderosm.generateNextTenderObjSequenceNo ( tenderid, "tender_boq_activity_section" );
                                            string boqactivitysectionInsquery = " insert into tender_boq_activity_section(fk_tender_id,obj_seq_no,parent_obj_seq_no,activity_section_no,workbook_name,worksheet_name,row_index,cell_reference,element_type,element_description) values(" + tenderid + "," + Activity_Section_objseq + ",' 10 ','20','" + workbookname + "','" + theWorkSheet.Name + "'," + therow.RowNumber ( ) + ",'"+thecell.Address+"','activity','" + thecell.Value + "');";
                                            objdb.ExecuteNonQuery ( CommandType.Text, boqactivitysectionInsquery );
                    }
                }
            }
        }
    

    您根据需要取值并将值插入数据库中。

    希望以上信息对您有用。请让我知道您的想法。

    谢谢 卡提克

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多