yl
2023-03-04 f355af6d5f8a6c8e80ec48b4458e9b33042f198e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
 
namespace VueWebApi.Tools
{
    public class NPOIHelper
    {
        /// <summary>
        /// NPOI简单Demo,快速入门代码
        /// </summary>
        /// <param name="dtSource"></param>
        /// <param name="strFileName"></param>
        /// <remarks>NPOI认为Excel的第一个单元格是:(0,0)</remarks>
        /// <Author>柳永法 http://www.yongfa365.com/ 2010-5-8 22:21:41</Author>
        public static void ExportEasy(DataTable dtSource, string strFileName)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet();
            //填充表头
            IRow dataRow = sheet.CreateRow(0);
            foreach (DataColumn column in dtSource.Columns)
            {
                dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
            }
 
 
            //填充内容
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                dataRow = sheet.CreateRow(i + 1);
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
                }
            }
 
 
            //保存
            using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(ms);
                    ms.Flush();
                    ms.Position = 0;
                    byte[] data = ms.ToArray();
                    fs.Write(data, 0, data.Length);
                    fs.Flush();
                }
            }
            //workbook.Dispose();
        }
 
 
 
 
        public static System.Data.DataTable GetExcelDatatable(string fileUrl)
        {
 
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + fileUrl + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
 
 
            DataSet myDataSet = new DataSet();
 
            //连接串  
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等   
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            //包含excel中表名的字符串数组  
            string[] strTableNames = new string[dtSheetName.Rows.Count];
            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
            }
            OleDbDataAdapter myCommand = null;
            DataTable dt = new DataTable();
            //从指定的表明查询数据,可先把所有表明列出来供用户选择  
            string strExcel = "select*from[" + strTableNames[0] + "]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            //myCommand.Fill(dt);  
            myCommand.Fill(myDataSet, "ExcelInfo");
            conn.Close();
            DataTable ExcelTable = myDataSet.Tables["ExcelInfo"].DefaultView.ToTable();
            return ExcelTable;
        }
 
 
        /// <summary>
        /// 多个sheet导入
        /// </summary>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public static List<DataTable> GetExcelDatatableList(string fileUrl)
        {
            List<DataTable> list = new List<DataTable>();
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + fileUrl + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
 
 
            DataSet myDataSet = new DataSet();
 
            //连接串  
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等   
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            //包含excel中表名的字符串数组  
            string[] strTableNames = new string[dtSheetName.Rows.Count];
            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
            }
            for (int i = 0; i < strTableNames.Length; i++)
            {
                OleDbDataAdapter myCommand = null;
                DataTable dt = new DataTable();
                //从指定的表明查询数据,可先把所有表明列出来供用户选择  
                string strExcel = "select*from[" + strTableNames[i].Trim() + "]";
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                //myCommand.Fill(dt);  
                myCommand.Fill(myDataSet, "" + i + "");
                conn.Close();
                DataTable ExcelTable = myDataSet.Tables[i].DefaultView.ToTable();
                list.Add(ExcelTable);
            }
 
            return list;
        }
 
        /// <summary>
        /// 多个sheet导入
        /// </summary>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public static List<DataTable> GetExcelDatatableListName(string fileUrl)
        {
            List<DataTable> list = new List<DataTable>();
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + fileUrl + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
 
 
            DataSet myDataSet = new DataSet();
 
            //连接串  
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等   
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            for (int m = 0; m < dtSheetName.Rows.Count; m++)
            {
                string TABLE_NAME = dtSheetName.Rows[m]["TABLE_NAME"].ToString();
                string FilterDatabase = "$FilterDatabase";
                if (TABLE_NAME.Contains(FilterDatabase))
                {
                    dtSheetName.Rows.Remove(dtSheetName.Rows[m]);
                }
            }
 
            //包含excel中表名的字符串数组  
            string[] strTableNames = new string[dtSheetName.Rows.Count];
            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
            }
            for (int i = 0; i < strTableNames.Length; i++)
            {
                OleDbDataAdapter myCommand = null;
                DataTable dt = new DataTable();
 
                //从指定的表明查询数据,可先把所有表明列出来供用户选择  
                string strExcel = "select*from[" + strTableNames[i].Trim() + "]";
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                //myCommand.Fill(dt);  
                myCommand.Fill(myDataSet, "" + i + "");
                conn.Close();
                DataTable ExcelTable = myDataSet.Tables[i].DefaultView.ToTable();
                ExcelTable.TableName = strTableNames[i].Trim();
                list.Add(ExcelTable);
            }
 
            return list;
        }
    }
}