|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
-
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.OleDb;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
-
-
- namespace BPASmartClient.Helper
- {
- public class ExcelHelper
- {
- public DataTable GetExcelToDataTableBySheet(string FileFullPath, string SheetName)
-
- {
-
- //此连接只能操作Excel2007之前(.xls)文件
-
- //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'";
-
- //此连接可以操作.xls与.xlsx文件
-
- string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
-
-
- OleDbConnection conn = new OleDbConnection(strConn);
-
- conn.Open();
-
- DataSet ds = new DataSet();
-
- OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", SheetName), conn);
-
- //OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("select * from [Sheet1$]", conn),conn);
-
- odda.Fill(ds, SheetName);
-
- conn.Close();
-
- return ds.Tables[0];
-
- }
- //根据Excel物理路径获取Excel文件中所有表名
-
- public String[] GetExcelSheetNames(string excelFile)
-
- {
- OleDbConnection objConn = null;
- System.Data.DataTable dt = null;
- try
- {
- //此连接只能操作Excel2007之前(.xls)文件
-
- //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + excelFile + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'";
-
- //此连接可以操作.xls与.xlsx文件
-
- string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + excelFile + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
-
- objConn = new OleDbConnection(strConn);
- objConn.Open();
- dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
-
- if (dt == null)
- {
- return null;
- }
- String[] excelSheets = new String[dt.Rows.Count];
- int i = 0;
- foreach (DataRow row in dt.Rows)
- {
- excelSheets[i] = row["TABLE_NAME"].ToString();
- i++;
- }
- return excelSheets;
- }
- catch
- {
- return null;
- }
- finally
- {
- if (objConn != null)
- {
- objConn.Close();
- objConn.Dispose();
- }
- if (dt != null)
- {
- dt.Dispose();
- }
- }
- }
-
- public static DataTable ReadDataFromCSV( string file)
- {
- Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
- DataTable dt = null;
- if(File.Exists(file))
- {
- dt = new DataTable();
- FileStream fs = new FileStream(file, FileMode.Open,FileAccess.Read);
- StreamReader sr;
-
- try
- {
- Encoding encoding = Encoding.Default;
- using (sr = new StreamReader(fs, Encoding.GetEncoding("GB2312")))
- {
- string head = sr.ReadLine();
- string[] headNames = head.Split(',');
- for (int i = 0; i < headNames.Length; i++)
- {
- dt.Columns.Add(headNames[i], typeof(string));
- }
- while (!sr.EndOfStream)
- {
- string lineStr = sr.ReadLine();
- if (lineStr != null)
- {
- string[] lines = lineStr.Split(',');
- DataRow dr = dt.NewRow();
- for (int i = 0; i < lines.Length; i++)
- {
- dr[i] = lines[i];
- }
- dt.Rows.Add(dr);
- }
- }
- sr.Close();
- fs.Close();
- }
-
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"读取csv文件异常:{ex.Message}");
- }
- }
- return dt;
-
- }
- }
-
-
- }
-
|