• 大小: 3.59MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-09
  • 语言: C#
  • 标签: .NET  

资源简介

我是新手一枚,做了个winform导入Excel到数据库的小东西,分享给大家,请大家指点下。 文件里面有我的导入的Excel文件,估计只能导这样的吧。不过改改还是能导别的数据的。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Text.Regularexpressions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;

namespace KQ
{
    class Excel : IDisposable
    {

        private string fileName = null;
        private IWorkbook workbook = null;
        private FileStream fs = null;
        private bool disposed;

        public Excel(string fileName)
        {
            this.fileName = fileName;
            disposed = false;

        }


        /// 
        /// 将excel中的数据导入到DataTable中
        /// 

        /// excel工作薄sheet的名称
        /// 第一行是否是DataTable的列名
        /// 返回的DataTable
        public DataTable ExcelToDataTable(string sheetName bool isFirstRowColumn)
        {

            ISheet sheet = null;
            DataTable data = new DataTable();
            int startRow = 0;
            try
            {
                fs = new FileStream(fileName FileMode.Open FileAccess.Read);
                if (fileName.IndexOf(“.xlsx“) > 0) // 2007版本
                    workbook = new XSSFWorkbook(fs);
                else if (fileName.IndexOf(“.xls“) > 0) // 2003版本
                    workbook = new HSSFWorkbook(fs);

                if (sheetName != null)
                {
                    sheet = workbook.GetSheet(sheetName);
                    if (sheet == null)
                    {
                        sheet = workbook.GetSheetAt(0);//如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                    }
                }
                else
                {
                    sheet = workbook.GetSheetAt(0);
                }

                //获取并显示设置 列
                if (sheet != null)
                {
                    //获取第一行,并设置为列
                    IRow firstRow = sheet.GetRow(0);
                    int cellCount = firstRow.LastCellNum;//一行最后一个cell的编号 即总的列数
                    if (isFirstRowColumn)
                    {
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                          ICell cell = firstRow.GetCell(i);
                            if (cell != null)
                            {
                                string cellValue = cell.StringCellValue;
                                if (cellValue != null)
                                {
                                    string[] s = Regex.Replace(cellValue.Trim() “\\s+“ “ “).Split(‘ ‘);//将多余的空格转成一个空格,然后变成数组
                                    for (int a = 0; a < s.Length; a++)
                                    {
                                        if (s[a].ToString() != null && s[a].ToString().Length != 0)
                                        {
                         

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件       39936  2016-07-04 14:17  CCCC3.xls
     目录           0  2016-07-06 16:38  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\
     目录           0  2016-07-06 16:37  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\
     目录           0  2016-07-06 16:31  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\
     目录           0  2016-07-06 16:31  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\
     文件      200704  2015-01-30 13:33  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\ICSharpCode.SharpZipLib.dll
     文件     1606144  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\NPOI.dll
     文件      423936  2015-02-22 18:17  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\NPOI.OOxml.dll
     文件      425038  2015-02-22 18:17  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\NPOI.OOxml.xml
     文件       85504  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\NPOI.Openxml4Net.dll
     文件      153267  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\NPOI.Openxml4Net.xml
     文件     1902592  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\NPOI.OpenxmlFormats.dll
     文件     2137432  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\NPOI.xml
     文件       13312  2016-07-06 16:37  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\WindowsFormsApplication59.exe
     文件       38400  2016-07-06 16:37  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\WindowsFormsApplication59.pdb
     文件       11600  2016-07-06 16:38  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\WindowsFormsApplication59.vshost.exe
     文件         490  2012-06-06 02:06  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\bin\Debug\WindowsFormsApplication59.vshost.exe.manifest
     目录           0  2016-07-06 16:31  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\
     文件      200704  2015-01-30 13:33  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\ICSharpCode.SharpZipLib.dll
     文件     1606144  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\NPOI.dll
     文件      423936  2015-02-22 18:17  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\NPOI.OOxml.dll
     文件      425038  2015-02-22 18:17  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\NPOI.OOxml.xml
     文件       85504  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\NPOI.Openxml4Net.dll
     文件      153267  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\NPOI.Openxml4Net.xml
     文件     1902592  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\NPOI.OpenxmlFormats.dll
     文件     2137432  2015-02-22 18:14  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\dll\NPOI.xml
     文件        5733  2016-07-06 11:54  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\Excel.cs
     文件           3  2016-07-06 08:25  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\Excel_2.cs
     文件        4395  2016-07-06 16:39  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\Form1.cs
     文件        3607  2016-07-06 16:31  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\Form1.Designer.cs
     文件        5817  2016-07-06 16:31  最新C#winform导入excel到DataTable里拆分好格式再导入SQL里\WindowsFormsApplication59\Form1.resx
............此处省略26个文件信息

评论

共有 条评论