• NPOI导入excel文件为DataTable,使用SqlBulkCopy添加到数据库表


    public DataTable ExcelToDataTable(Stream stream, string fileName)
            {
                DataTable data = new DataTable();
                try
                {
                    IWorkbook workbook = null;
                    if (fileName.IndexOf(".xlsx") > 0)
                        workbook = new XSSFWorkbook(stream);
                    else if (fileName.IndexOf(".xls") > 0)
                        workbook = new HSSFWorkbook(stream);
                    //sheet = workbook.GetSheet(sheetName);
                    ISheet sheet = workbook.GetSheetAt(0);
                    if (sheet != null)
                    {
                        IRow firstRow = sheet.GetRow(0);
                        int cellCount = firstRow.LastCellNum;
    
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            ICell cell = firstRow.GetCell(i);
                            if (cell != null)
                            {
                                string cellValue = cell.StringCellValue;
                                if (cellValue != null)
                                {
                                    DataColumn column = new DataColumn(cellValue);
                                    data.Columns.Add(column);
                                }
                            }
                        }
    
                        int startRow = sheet.FirstRowNum + 1;
                        int rowCount = sheet.LastRowNum;
                        for (int i = startRow; i <= rowCount; ++i)
                        {
                            IRow row = sheet.GetRow(i);
                            if (row == null) continue;
    
                            DataRow dataRow = data.NewRow();
                            for (int j = row.FirstCellNum; j < cellCount; ++j)
                            {
                                if (row.GetCell(j) != null)
                                    dataRow[j] = row.GetCell(j).ToString();
                            }
                            data.Rows.Add(dataRow);
                        }
                    }
                    return data;
                }
                catch
                {
                    return null;
                }
            }
    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Data source=.; Database=Test; UID=sa; password=123;"))
                        {
                            conn.Open();
                            using (System.Data.SqlClient.SqlBulkCopy bulk = new System.Data.SqlClient.SqlBulkCopy(conn))
                            {
                                bulk.DestinationTableName = "Import";
                                bulk.BatchSize = dt.Rows.Count;
                                bulk.ColumnMappings.Add("ID", "ID");
                                bulk.ColumnMappings.Add("Sex", "Sex");
                                bulk.ColumnMappings.Add("Name", "Name");
                                bulk.ColumnMappings.Add("Age", "Age");
                                bulk.WriteToServer(dt);
                            }
                        }

    表格数据(导入到Test数据库,Import表):

  • 相关阅读:
    11C++11通用为本,专用为末_2
    10C++11通用为本,专用为末_1
    09C++11保证稳定性和兼容性
    21变量名的力量_2
    NOIP2018 游记
    CF767C 经典的树形DP
    CF1A Theatre Square
    洛谷P1720 月落乌啼算钱
    洛谷P3388 缩点
    NOIP2017D2T1 奶酪 洛谷P3958
  • 原文地址:https://www.cnblogs.com/talentzemin/p/6816778.html
Copyright © 2020-2023  润新知