RT,请教一下各位.
我在网上找了好些代码,都感觉不靠谱,想要切实可行的,谢谢了!
我在网上找了好些代码,都感觉不靠谱,想要切实可行的,谢谢了!
解决方案 »
- C#界面,用Openfiledialog打开一个txt后,如何分行读?还怎么用StreamReader命令了?
- 我会C#,但水平不高,再学PHP会不会把语法弄混?
- 求可以播放Word文档的flash
- 如何在winform程序中的标签页上显示一个关闭按纽?
- C#实现深复制有几种方法?
- 请教一个C#对WORD操作的问题
- DataGridView(Winform)双击表头时如何不触发这个DataGridView的双击事件
- B界面中的值 怎么在A界面中的datagridview中显示出来
- 求一个发邮件时正文可以嵌入图片的例子
- 微软发布“Visual Studio.Net”更新计划
- 求办公协同系统项目文件
- sql 中between的一些疑问
Order是一个类型吗?不对啊...
不考虑 字段
/// 将Excel保存到DataSet
/// </summary>
/// <param name="file">Excel表名</param>
/// <returns></returns>
private static DataSet ImportExcel(string file)
{
DataSet dsExcel = new DataSet();
FileInfo fileinto = new FileInfo(file);
if (!fileinto.Exists)
return null;
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file +
";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
try
{
conn.Open();
string strSql = "select * from [社区表$]";
OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);
da.Fill(dsExcel);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
return dsExcel;
}
你的代码基本没问题. public void setExcelFileAsDataSourceToDataGridView(string FileName)
{
OleDbConnection objConn;
OleDbDataAdapter oleDA;
DataSet ds;
//Check Whether file is xls file or not
if (Path.GetExtension(FileName) == ".xls")
{
try
{
//Create a OLEDB connection for Excel file
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + FileName + ";" + "Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(connectionString);
oleDA = new OleDbDataAdapter("select * from [Sheet1$]", objConn);
ds = new DataSet();
//Fill the Data Set
oleDA.Fill(ds);
//Set DataSource of DataGridView
dataGridView1.DataSource = ds.Tables[0];
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = i + 1;
}
ds.Dispose();
oleDA.Dispose();
objConn.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Please select Excel File");
}
}