c#

位置:IT落伍者 >> c# >> 浏览文章

C#怎样用excel.dll读取excel文件


发布日期:2022年08月09日
 
C#怎样用excel.dll读取excel文件

public DataTable ReadExcelToTable(string filepath)

{

string excelfilePath = ServerMapPath(Excel/Excelxls);

ExcelApplication myExcel = new ExcelApplicationClass();

ExcelWorkbooks myBooks = myExcelApplicationWorkbooks;

object oMissing = SystemReflectionMissingValue;

ExcelWorkbook myBook = myBooksOpen(excelfilePath oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing);

//ExcelWorkbook myBook = myExcelWorkbooks[];

int sheetint = myBookWorksheetsCount;//能得到sheet的数量

ExcelWorksheet mySheet = (ExcelWorksheet)myBookWorksheets[];

int rowsint = mySheetUsedRangeCellsRowsCount; //得到行数

int columnsint = mySheetUsedRangeCellsColumnsCount;//得到列数

SystemDataDataTable dt = new SystemDataDataTable(mytable);

for (int i = ; i < columnsint; i++)

{

dtColumnsAdd(F + iToString() SystemTypeGetType(SystemString));

}

DataSet myDs = new DataSet();

myDsTablesAdd(dt);

DataRow myRow;

myDsClear();

for (int i = ; i < rowsint; i++) //第一行为标题不读取

{

myRow = myDsTables[mytable]NewRow();

for (int j = ; j < columnsint; j++)

{

ExcelRange r = (ExcelRange)mySheetCells[i j];

string strValue = rTextToString();

string aa = strValue;

string columnname = F + jToString();

myRow[columnname] = strValue;

}

myDsTables[mytable]RowsAdd(myRow);

}

return myDsTables[mytable];

}

               

上一篇:.NET导出Gridview到excel 带模板列显示

下一篇:C#中读取数据库中Image数据