[英]How prevent to create a .exe.config in a Windows Forms Application build (Visual Studio Community 2019 - C#)
[英]Excel 2016 to DataGridView in Windows Forms Visual Studio 2019 community
我使用 Excel 2016,Visual Studio 2019 社区并希望将 Excel 工作表导出到数据网格视图中。 另外我想 select 某些列然后将被采用。 程序运行到填充数据集的function 然后程序运行到catch而不显示错误信息
private void Cmd_Fbd_Data_List_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog1 = new OpenFileDialog(); //create openfileDialog Object
openFileDialog1.Filter = "XML Files (*.xml; *.xls; *.xlsx; *.xlsm; *.xlsb) |*.xml; *.xls; *.xlsx; *.xlsm; *.xlsb";//open file format define Excel Files(.xls)|*.xls| Excel Files(.xlsx)|*.xlsx|
openFileDialog1.FilterIndex = 3;
openFileDialog1.Multiselect = false; //not allow multiline selection at the file selection level
openFileDialog1.Title = "Open Text File-R13"; //define the name of openfileDialog
openFileDialog1.InitialDirectory = @"Desktop"; //define the initial directory
if (openFileDialog1.ShowDialog() == DialogResult.OK) //executing when file open
{
string pathName = openFileDialog1.FileName;
string fileName = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
DataTable tbContainer = new DataTable();
string strConn = string.Empty;
FileInfo file = new FileInfo(pathName);
if (!file.Exists) { throw new Exception("Error, file doesn't exists!"); }
string extension = file.Extension;
switch (extension)
{
case ".xls":
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
break;
case ".xlsx":
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
break;
default:
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
break;
}
String name = "DPL";// is the Name of the first Sheet
OleDbConnection con = new OleDbConnection(strConn);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
// up to this line the programm works after that the Exception shows up
sda.Fill(data);
Dgv_Data_List.DataSource = data;
}
}
catch (Exception)
{
MessageBox.Show("Error!");
}
}
我只是在检查OleDbDataAdapter.Fill 的 MSDN 文档,它说它需要一个 ADODBRecordSet 作为第二个参数。 这里不是这样吗? 查看文档中的示例,看看这种方式而不是使用 OleDbCommand 是否适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.