private void btnInput_Click(object sender, EventArgs e)//导入按钮点击事件
{
openFileDialog1.Filter = "Microsoft Office Excel 工作薄(*.xls)|*.xls";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
DataTable dt1 = GetExcel(openFileDialog1.FileName);//把Excel中的数据导入到DataTable中
}
}
public DataTable GetExcel(string Path)//Excel导入到DataTable
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = null;
DataTable dt = new DataTable();
int result = 1;
try
{
conn.Open();
DataTable dtName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string strExcel = "Select * from [" + dtName.Rows[0]["TABLE_NAME"].ToString() + "]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(dt);
conn.Close();
}
catch (Exception ex)
{
result = 2;
}
finally
{
conn.Close();
}
if (result == 2) return null;
else return dt;
}
已经写到DataTable中了.这个你输出应该没问题了吧.
看你什么list arryList 还是ILIST 可以直接循环UserRange,可以吧问题描述更清楚一点
下面的代码希望对你有帮助
//创建Excel实例
Excel.Application app = new Excel.Application();
//打开Excel文件
Excel._Workbook wb1 = app.Workbooks.Open(ExcelUrl, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//获取第一个Sheet
Excel._Worksheet shet = (Excel._Worksheet)wb1.Worksheets[1];
//声明Excel单元格
Excel.Range myRange = shet.UsedRange;
下面就是循环 myRange 里面的数据就可以了