110,499
社区成员
发帖
与我相关
我的任务
分享
public System.Data.DataTable ExcelToDataTable(string strExcelFileName)
{
string sheet = GetExcelSheetNames(strExcelFileName);
//根据路径打开一个Excel文件并将数据填充到DataSet中
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strExcelFileName + ";Extended Properties = 'Excel 12.0 Xml;HDR=yes'";
string strExcel = "";
strExcel = "select * from [sheet1$]";
OleDbConnection conn = null;
DataSet ds = new DataSet();
try
{ using (conn = new OleDbConnection(strConn))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, "table1");
conn.Close();
}
return ds.Tables["table1"];
}
catch(Exception)
{
throw;
}
finally
{
// Clean up.
if (conn != null)
{
conn.Close();
conn.Dispose();
}
if (ds != null)
{
ds.Dispose();
}
}
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells["time"].Value = DateTime.Parse(dataGridView1.Rows[i].Cells["time"].Value.ToString()).ToString("yyyy年m月d日");
}
dataGridView1.Columns[1].DefaultCellStyle.Format = "yyyy年m月d日";