打开Excel文件使用ClosedXML错误

时间:2016-01-13 00:33:02

标签: asp.net vb.net excel closedxml

我正在尝试使用ClosedXML在ASP.NET中打开一个6MB的Excel文件,但是我收到一条错误,说"隐式转换错误。无法转换null对象。"

这是我的代码:

public Task<long> MaxAsync(Expression<Func<T, bool>> predicate, 
       Expression<Func<T, long>> projection) 
{
    return this.GetFindQuery(predicate, null, null).AsScalarAsync().Max(projection);
}

来源:&#34; DocumentFormat.OpenXml&#34; 我也尝试打开现有的Excel文件,但仍然会导致此错误。还尝试将文件放在不同的目录中,只是因为我的驱动器的许可,没有运气。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

// boqserverfilepath下面是指excel文件存储的文件夹EX:&#34;〜/ Uploaded_Boq /&#34 ;;

        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 );
                }
            }
        }
    }

根据您的要求获取值,并将值插入数据库中。

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

感谢 KARTHIK