2016-01-13 183 views
1

我想在ASP.NET中使用ClosedXML打開一個6MB的Excel文件,但我得到一個錯誤,說「隱式轉換錯誤,無法轉換空對象。打開Excel文件使用ClosedXML錯誤

這裏是我的代碼:

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文件時,它仍然會導致這個錯誤。還嘗試把文件放在不同的目錄中,認爲這只是因爲我的驅動器的許可,根本沒有運氣。提前致謝。

回答

0

下面是用於獲取excel文件在封閉XML

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

您根據您的要求取值並在數據庫中插入值。

希望以上信息會有用。請讓我知道你的想法。

謝謝 karthik