asp.net reads Excel data to DataTable code
- 2020-05-09 18:23:31
- OfStack
/// <summary>
/// Gets the specified path, specified workbook name Excel data : Take the first 1 a sheet The data of
/// </summary>
/// <param name="FilePath"> File storage path </param>
/// <param name="WorkSheetName"> Workbook name </param>
/// <returns> If you try to find the data, it will be returned 1 A complete Table Otherwise, an exception is returned </returns>
public DataTable GetExcelData(string astrFileName)
{
string strSheetName = GetExcelWorkSheets(astrFileName)[0].ToString();
return GetExcelData(astrFileName, strSheetName);
}
code
/// <summary>
/// Returns a list of workbooks contained in the specified file ; If you have WorkSheet , returns the one named after the workbook ArrayList Otherwise return null
/// </summary>
/// <param name="strFilePath"> To obtain the Excel</param>
/// <returns> If you have WorkSheet , returns the one named after the workbook ArrayList Otherwise return null </returns>
public ArrayList GetExcelWorkSheets(string strFilePath)
{
ArrayList alTables = new ArrayList();
OleDbConnection odn = new OleDbConnection(GetExcelConnection(strFilePath));
odn.Open();
DataTable dt = new DataTable();
dt = odn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
throw new Exception(" Unable to get designation Excel Architecture. ");
}
foreach (DataRow dr in dt.Rows)
{
string tempName = dr["Table_Name"].ToString();
int iDolarIndex = tempName.IndexOf('$');
if (iDolarIndex > 0)
{
tempName = tempName.Substring(0, iDolarIndex);
}
// Fixed a Excel2003 Some workbook names for Chinese characters table cannot be correctly identified BUG .
if (tempName[0] == '\'')
{
if (tempName[tempName.Length - 1] == '\'')
{
tempName = tempName.Substring(1, tempName.Length - 2);
}
else
{
tempName = tempName.Substring(1, tempName.Length - 1);
}
}
if (!alTables.Contains(tempName))
{
alTables.Add(tempName);
}
}
odn.Close();
if (alTables.Count == 0)
{
return null;
}
return alTables;
}
code
/// <summary>
/// Gets the specified path, specified workbook name Excel data
/// </summary>
/// <param name="FilePath"> File storage path </param>
/// <param name="WorkSheetName"> Workbook name </param>
/// <returns> If you try to find the data, it will be returned 1 A complete Table Otherwise, an exception is returned </returns>
public DataTable GetExcelData(string FilePath, string WorkSheetName)
{
DataTable dtExcel = new DataTable();
OleDbConnection con = new OleDbConnection(GetExcelConnection(FilePath));
OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [" + WorkSheetName + "$]", con);
// read
con.Open();
adapter.FillSchema(dtExcel, SchemaType.Mapped);
adapter.Fill(dtExcel);
con.Close();
dtExcel.TableName = WorkSheetName;
// return
return dtExcel;
}
code
/// <summary>
/// Get the link string
/// </summary>
/// <param name="strFilePath"></param>
/// <returns></returns>
public string GetExcelConnection(string strFilePath)
{
if (!File.Exists(strFilePath))
{
throw new Exception(" The specified Excel The file does not exist! ");
}
return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";Extended properties=\"Excel 8.0;Imex=1;HDR=Yes;\"";
//@"Provider=Microsoft.Jet.OLEDB.4.0;" +
//@"Data Source=" + strFilePath + ";" +
//@"Extended Properties=" + Convert.ToChar(34).ToString() +
//@"Excel 8.0;" + "Imex=1;HDR=Yes;" + Convert.ToChar(34).ToString();
}