部分代码如下: 1.(根据用户选择下拉列表框里的值 填充数据集) 2.DotNetBar.ComboBoxItem 第3方控件就是combobox
public void OpenFile(DevComponents.DotNetBar.ComboBoxItem cmb)//打开Excel文件,得到里面的表名,填充到 下拉列表框 里
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开(Open)";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofd.Filter = "文本文件(*.xls)|*.xls";
ofd.ValidateNames = true;
ofd.CheckFileExists = true; //验证文件是否存在
ofd.CheckPathExists = true; // 验证文件路径是否存在
ofd.Multiselect = false;
ofd.RestoreDirectory = false;
try
{
if (ofd.ShowDialog() == DialogResult.OK)
{
if (ofd.FileName.Split('.')[1].ToString().Equals("xls")) //判断文件的后缀名是否为xls
{
object missing = System.Reflection.Missing.Value;
Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Open(ofd.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
cmb.Items.Clear();
for (int i = 0; i < excelApp.Workbooks[1].Worksheets.Count; i++)
{
Excel.Worksheet ws = (Excel.Worksheet)excelApp.Workbooks[1].Worksheets[i + 1];
string sSheetName = ws.Name;
cmb.Items.Add(sSheetName);
}
cmb.SelectedIndex = 0;
}
else
{
MessageBox.Show("请选择Excel文件!");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
} public DataSet ExcelToDataSet(string opnFileName, string tableName) //把Excel转换成DataSet
{
OleDbConnection objcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+opnFileName+";" + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");
OleDbDataAdapter objdataAdapter = new OleDbDataAdapter("select * from [" + tableName + "$]", objcon);
DataSet ds = new DataSet();
objdataAdapter.Fill(ds); -----------到了这里 程序阻塞了
return ds;
}在线等!
public void OpenFile(DevComponents.DotNetBar.ComboBoxItem cmb)//打开Excel文件,得到里面的表名,填充到 下拉列表框 里
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开(Open)";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofd.Filter = "文本文件(*.xls)|*.xls";
ofd.ValidateNames = true;
ofd.CheckFileExists = true; //验证文件是否存在
ofd.CheckPathExists = true; // 验证文件路径是否存在
ofd.Multiselect = false;
ofd.RestoreDirectory = false;
try
{
if (ofd.ShowDialog() == DialogResult.OK)
{
if (ofd.FileName.Split('.')[1].ToString().Equals("xls")) //判断文件的后缀名是否为xls
{
object missing = System.Reflection.Missing.Value;
Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Open(ofd.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
cmb.Items.Clear();
for (int i = 0; i < excelApp.Workbooks[1].Worksheets.Count; i++)
{
Excel.Worksheet ws = (Excel.Worksheet)excelApp.Workbooks[1].Worksheets[i + 1];
string sSheetName = ws.Name;
cmb.Items.Add(sSheetName);
}
cmb.SelectedIndex = 0;
}
else
{
MessageBox.Show("请选择Excel文件!");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
} public DataSet ExcelToDataSet(string opnFileName, string tableName) //把Excel转换成DataSet
{
OleDbConnection objcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+opnFileName+";" + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");
OleDbDataAdapter objdataAdapter = new OleDbDataAdapter("select * from [" + tableName + "$]", objcon);
DataSet ds = new DataSet();
objdataAdapter.Fill(ds); -----------到了这里 程序阻塞了
return ds;
}在线等!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货