c#编程 OpenFileDialog 如何使用Dataview打开导入一个excel文件
voidButton1Click(objectsender,System.EventArgse){OpenFileDialogotext=newOpenFileDialo...
void Button1Click(object sender, System.EventArgs e)
{
OpenFileDialog otext=new OpenFileDialog();
otext.CheckFileExists=true;
otext.CheckPathExists=true;
otext.Filter="excel filer(*.xls)|*.xls";
if(otext.ShowDialog()==DialogResult.OK)
{
}
else
{
return;
}
}
可以打开路径 选择execl 求解答 如何导入,
应该是
if(otext.ShowDialog()==DialogResult.OK)
{
}
里面加些什么吧~
求共同进步 展开
{
OpenFileDialog otext=new OpenFileDialog();
otext.CheckFileExists=true;
otext.CheckPathExists=true;
otext.Filter="excel filer(*.xls)|*.xls";
if(otext.ShowDialog()==DialogResult.OK)
{
}
else
{
return;
}
}
可以打开路径 选择execl 求解答 如何导入,
应该是
if(otext.ShowDialog()==DialogResult.OK)
{
}
里面加些什么吧~
求共同进步 展开
4个回答
展开全部
加引用Microsoft office core
public class ExcelOperation
{
static Object objMiss = Missing.Value;
public static System.Data.DataTable Open(String fileName, int sheetIndex, int titleRow, int StartRow)
{
Application app = new ApplicationClass();
app.Workbooks.Open(fileName, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss);
Workbook myWorkbook = (Workbook)app.Workbooks[1];
if (sheetIndex <= 0) sheetIndex = 1;
Worksheet myWorksheet = (Worksheet)myWorkbook.Sheets[sheetIndex];
System.Data.DataTable myTable = new System.Data.DataTable();
Range myRange;
String strColumnName;
for (int i = 1; i <= myWorksheet.UsedRange.Columns.Count; i++)
{
strColumnName = "Col" + i;
if (titleRow > 0)
{
myRange = (Range)myWorksheet.Cells[titleRow, i];
if (myRange.Value2 != null)
strColumnName = myRange.Value2.ToString();
}
if (myTable.Columns.Contains(strColumnName))
{
strColumnName = strColumnName + 1;
}
myTable.Columns.Add(strColumnName);
}
myTable.AcceptChanges();
if (StartRow <= 0) StartRow = 1;
try
{
for (int i = StartRow; i <= myWorksheet.UsedRange.Rows.Count; i++)
{
DataRow drAdd = myTable.NewRow();
for (int j = 1; j <= myWorksheet.UsedRange.Columns.Count; j++)
{
myRange = (Range)myWorksheet.Cells[i, j];
if (myRange.Value2 != null)
drAdd[j - 1] = myRange.Value2.ToString();
}
myTable.Rows.Add(drAdd);
}
}
finally
{
myWorksheet = null;
myWorkbook.Close(objMiss, objMiss, objMiss);
myWorkbook = null;
app.Quit();
app = null;
GC.Collect();
}
myTable.AcceptChanges();
return myTable;
}
public class ExcelOperation
{
static Object objMiss = Missing.Value;
public static System.Data.DataTable Open(String fileName, int sheetIndex, int titleRow, int StartRow)
{
Application app = new ApplicationClass();
app.Workbooks.Open(fileName, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss);
Workbook myWorkbook = (Workbook)app.Workbooks[1];
if (sheetIndex <= 0) sheetIndex = 1;
Worksheet myWorksheet = (Worksheet)myWorkbook.Sheets[sheetIndex];
System.Data.DataTable myTable = new System.Data.DataTable();
Range myRange;
String strColumnName;
for (int i = 1; i <= myWorksheet.UsedRange.Columns.Count; i++)
{
strColumnName = "Col" + i;
if (titleRow > 0)
{
myRange = (Range)myWorksheet.Cells[titleRow, i];
if (myRange.Value2 != null)
strColumnName = myRange.Value2.ToString();
}
if (myTable.Columns.Contains(strColumnName))
{
strColumnName = strColumnName + 1;
}
myTable.Columns.Add(strColumnName);
}
myTable.AcceptChanges();
if (StartRow <= 0) StartRow = 1;
try
{
for (int i = StartRow; i <= myWorksheet.UsedRange.Rows.Count; i++)
{
DataRow drAdd = myTable.NewRow();
for (int j = 1; j <= myWorksheet.UsedRange.Columns.Count; j++)
{
myRange = (Range)myWorksheet.Cells[i, j];
if (myRange.Value2 != null)
drAdd[j - 1] = myRange.Value2.ToString();
}
myTable.Rows.Add(drAdd);
}
}
finally
{
myWorksheet = null;
myWorkbook.Close(objMiss, objMiss, objMiss);
myWorkbook = null;
app.Quit();
app = null;
GC.Collect();
}
myTable.AcceptChanges();
return myTable;
}
展开全部
string strCon = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + excelPath + "'" + ";Extended Properties=Excel 8.0"; //Excel的连接字符串 XLS_excelPath 你的excel文件路径,不解释
OleDbConnection myConn = new OleDbConnection(strCon);//创建一个OLEDBC的链接
myConn.Open();//打开数据库的连接,得到一个数据集
DataSet myDataSet = new DataSet();
string StrSql = "Select * From [" + StyleSheet + "$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
myCommand.Dispose();
DataTable dt = myDataSet.Tables["[" + StyleSheet + "$]"];
myConn.Close();//关闭数据库连接
myCommand.Dispose();
DataGrid1.DataSource = dt.DefaultView;//用来测试Excel中的数据是否读取成功
DataGrid1.DataBind();
OleDbConnection myConn = new OleDbConnection(strCon);//创建一个OLEDBC的链接
myConn.Open();//打开数据库的连接,得到一个数据集
DataSet myDataSet = new DataSet();
string StrSql = "Select * From [" + StyleSheet + "$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
myCommand.Dispose();
DataTable dt = myDataSet.Tables["[" + StyleSheet + "$]"];
myConn.Close();//关闭数据库连接
myCommand.Dispose();
DataGrid1.DataSource = dt.DefaultView;//用来测试Excel中的数据是否读取成功
DataGrid1.DataBind();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
打开excel不是问题,问题在于:你是想把excel显示在dataview控件中????
这个就麻烦了。。。。
思路1: 直接将excel显示在dataview中,这个恐怕得在dataview中嵌入什么东西
思路2: 将excel的内容显示到dataview中,不加载格式。 这倒是有点希望,只
是这个也涉及到excel编程操作。
这个就麻烦了。。。。
思路1: 直接将excel显示在dataview中,这个恐怕得在dataview中嵌入什么东西
思路2: 将excel的内容显示到dataview中,不加载格式。 这倒是有点希望,只
是这个也涉及到excel编程操作。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ExcelDataReader可以直接读取excel为DataTable,这个类库你可以去codeplex上搜索,其他的我想你应该知道怎么做
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询