简体   繁体   中英

Import Excel Sheet in Asp.Net

I need to ask about import excel sheet question with child-parent relationship.

below i have a sample table 在此输入图像描述

then I need to import this table like that this format -->

        IndustryName | IndustryCode | CategoryName | CategoryCode | CategoryValue | SubCategoryName | SubCategoryCode | SubCategoryValue
        IndustryName1  IndustCode     CatName        CatCode        CatValue        SubCatName        SubCatCode        SubCatValue
        IndustryName1  IndustCode     CatName        CatCode        CatValue        SubCatName        SubCatCode        SubCatValue

On the SubCategory line There are two spaces in front of SubCategoryCode.Its Fixed.

Anybody Have an idea how can i make it ?

You can import your excel data to a DataSet and then play around with dataset and create your desired table. For eg when iterating through the first column you can check the string if it has 2 spaces before then add it to subcategory column and others to the category column. All you have to do is iterate through the rows of the DataSet and store your required values in a new datatable.

public DataSet ExcelToDS(string Path, string sheetname)
        {
            string xlsConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0;";
            string xlsxConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=Excel 12.0";
            using (OleDbConnection conn = new OleDbConnection(xlsxConn))
            {
                conn.Open();
                string strExcel = "select * from [" + sheetname + "$]";
                OleDbDataAdapter oledbda = new OleDbDataAdapter(strExcel, xlsxConn);
                DataSet ds = new DataSet();
                oledbda.Fill(ds, sheetname);
                conn.Close();
                return ds;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM