导入代码如下
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();
}
}
}
利用如上的代码 将excel导入到datagridview
如果excel中的日期格式为“2009年3月5日”,导入进去后就变成了“2009-3-5”,如何才能让导入的格式不发生变化仍然保持为“2009年3月5日”!谢谢各位!!!
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();
}
}
}
利用如上的代码 将excel导入到datagridview
如果excel中的日期格式为“2009年3月5日”,导入进去后就变成了“2009-3-5”,如何才能让导入的格式不发生变化仍然保持为“2009年3月5日”!谢谢各位!!!
解决方案 »
- C# io 流问题?
- 在vs环境中用c# + vtk怎么做鼠标交互???
- 大家好,这两个路径有什么区别啊??
- C#中 加密 解密问题,高手快来帮忙啊!!
- 如何将BindingList<ClassA>转换成BindList<Object>
- Winform 中 如何让用户编辑文本(就像FCKEditor在web中的效果),并保存到数据库中.
- 这个弹出窗口如何写?
- c# webbrowse 添加收藏夹的源程序
- 计算机管理方案求各位大侠给个资料
- 如何得到一个目录下所有的*.exe文件,包括子目录的.
- fastReport1.2.79 动态数据源 请问如何绑定?
- webservice异步调用完毕后,如何将返回的数据绑定至winform的控件?
dataGridView1.Columns[1].DefaultCellStyle.Format = "yyyy年m月d日";
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.Rows[i].Cells["time"].Value = DateTime.Parse(dataGridView1.Rows[i].Cells["time"].Value.ToString()).ToString("yyyy年m月d日");
}