Closed XML To Read Excel Files In ASP.NET C#

Datetime:2016-08-22 22:09:05          Topic: ASP.NET  XML  C#           Share

Namespace

using ClosedXML.Excel;

Code

public void StartBoqWorkBookImport()   
{  
    workbookProcessing("Maram_Section.xlsx");  
}  
private void workbookProcessing(string workbookname) {  
    SqlDatabase objdb = new SqlDatabase(OSMC.constring_Property);  
    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);  
            }  
        }  
    }  
}  

Diagrammatic Representation for Collections Hierarchy in ClosedXML to access the data:

When you compare it to the OpenXML, it is similar (just for your additional information).

Kindly referhere. Read Excel files, using OpenXML to know the basics about OpenXML.

Conclusion

I hope this will be useful for the first time users of closed XML. Kindly, let me know your feedback or thoughts.





About List