C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable
C#實(shí)現(xiàn)EXCEL表格轉(zhuǎn)DataTable
C#代碼實(shí)現(xiàn)把Excel文件轉(zhuǎn)化為DataTable,根據(jù)Excel的文件后綴名不同,用不同的方法來進(jìn)行實(shí)現(xiàn),下面通過根據(jù)Excel文件的兩種后綴名(*.xlsx和*.xls)分別來實(shí)現(xiàn)。獲取文件后綴名的方法是:Path.GetExtension(fileName)方法,通過引用:using System.IO;實(shí)現(xiàn)代碼如下:(其中以下代碼中出現(xiàn)的filename都是帶盤符的絕對(duì)路徑)
根據(jù)Excel文件的后綴名不同調(diào)用的主方法
private DataTable FileToDataTable(string fileName)
{
DataTable dt = new DataTable();
string extendName = Path.GetExtension(fileName);//獲取文件的后綴名
switch (extendName.ToLower())
{
case ".xls":
dt = XlsToDataTable(fileName);
break;
case ".xlsx":
dt = XlsxToDataTable(fileName);
break;
default:
break;
}
return dt;
}XlsToDataTable()
private DataTable XlsToDataTable(string fileName)
{
DataTable dataTable = new DataTable();
Stream stream = null;
try
{
stream = File.OpenRead(fileName);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(stream);
HSSFSheet hssfsheet = (HSSFSheet)hssfworkbook.GetSheetAt(hssfworkbook.ActiveSheetIndex);
HSSFRow hssfrow = (HSSFRow)hssfsheet.GetRow(0);
int lastCellNum = (int)hssfrow.LastCellNum;
for (int i = (int)hssfrow.FirstCellNum; i < lastCellNum; i++)
{
DataColumn column = new DataColumn(hssfrow.GetCell(i).StringCellValue);
dataTable.Columns.Add(column);
}
dataTable.TableName = hssfsheet.SheetName;
int lastRowNum = hssfsheet.LastRowNum;
//列名后,從TABLE第二行開始進(jìn)行填充數(shù)據(jù)
for (int i = hssfsheet.FirstRowNum + 1; i < hssfsheet.LastRowNum; i++)//
{
HSSFRow hssfrow2 = (HSSFRow)hssfsheet.GetRow(i);
DataRow dataRow = dataTable.NewRow();
for (int j = (int)hssfrow2.FirstCellNum; j < lastCellNum; j++)//
{
dataRow[j] = hssfrow2.GetCell(j);//
}
dataTable.Rows.Add(dataRow);
}
stream.Close();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(Page, GetType(), "alertForm", "alert(' Xls to DataTable: " + ex.Message + "');", true);
}
finally
{
if (stream != null)
{
stream.Close();
}
}
return dataTable;
}XlsxToDataTable()
public DataTable XlsxToDataTable(string vFilePath)
{
DataTable dataTable = new DataTable();
try
{
SLDocument sldocument = new SLDocument(vFilePath);
dataTable.TableName = sldocument.GetSheetNames()[0];
SLWorksheetStatistics worksheetStatistics = sldocument.GetWorksheetStatistics();
int startColumnIndex = worksheetStatistics.StartColumnIndex;
int endColumnIndex = worksheetStatistics.EndColumnIndex;
int startRowIndex = worksheetStatistics.StartRowIndex;
int endRowIndex = worksheetStatistics.EndRowIndex;
for (int i = startColumnIndex; i <= endColumnIndex; i++)
{
SLRstType cellValueAsRstType = sldocument.GetCellValueAsRstType(1, i);
dataTable.Columns.Add(new DataColumn(cellValueAsRstType.GetText(), typeof(string)));
}
for (int j = startRowIndex + 1; j <= endRowIndex; j++)
{
DataRow dataRow = dataTable.NewRow();
for (int i = startColumnIndex; i <= endColumnIndex; i++)
{
dataRow[i - 1] = sldocument.GetCellValueAsString(j, i);
}
dataTable.Rows.Add(dataRow);
}
}
catch (Exception ex)
{
throw new Exception("Xlsx to DataTable: \n" + ex.Message);
}
return dataTable;
}到此這篇關(guān)于C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#如何將DataTable導(dǎo)出到Excel解決方案
- C#操作EXCEL DataTable轉(zhuǎn)換的實(shí)例代碼
- C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet
- C#實(shí)現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法
- C#使用Datatable導(dǎo)出Excel
- C#中DataGridView導(dǎo)出Excel的兩種方法
- C#把DataTable導(dǎo)出為Excel文件
- C#實(shí)現(xiàn)讀取Excel文件并將數(shù)據(jù)寫入數(shù)據(jù)庫和DataTable
- C#中ExcelDataReader的具體使用
相關(guān)文章
C#關(guān)聯(lián)自定義文件類型到應(yīng)用程序并實(shí)現(xiàn)自動(dòng)導(dǎo)入功能
今天通過本文給大家分享C#關(guān)聯(lián)自定義文件類型到應(yīng)用程序并實(shí)現(xiàn)自動(dòng)導(dǎo)入功能,代碼中寫入了兩個(gè)注冊(cè)表,實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09

