Winform中Excel导入到指定的表中,效果如图所示:
把制作的Excel表在Winform下导入到SQLServer中的“Test”表中,应该怎样实现???图中使用的Winform中的什么控件啊,“OpenFileDialog”、"SaveFileDialog"和"FolderBrowerDialog"控件好像都不行呢???
把制作的Excel表在Winform下导入到SQLServer中的“Test”表中,应该怎样实现???图中使用的Winform中的什么控件啊,“OpenFileDialog”、"SaveFileDialog"和"FolderBrowerDialog"控件好像都不行呢???
然后自己实现导入代码。
你哪个步骤不会?
你哪个步骤不会?
思路正确,就是没有代码实例,遗憾
private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Excel 文件(*.xls;*.xls)|*.xls;*.xls";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
tbFileName.Text = openFileDialog1.FileName;
}
}2、
private void btnRecive_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel 文件(*.xls;*.xls)|*.xls;*.xls";//"Excel 文件(*.xlsx)|*.xlsx";
DialogResult dialogResult1 = saveFileDialog1.ShowDialog();
Application.DoEvents(); if (dialogResult1 == DialogResult.OK)
{
string fileName = saveFileDialog1.FileName;
if (fileName != "")
{
CreateReport(fileName, DateType);
}
}
}
3、// 读数据
string fileName = tbFileName.Text; DataTable dt = new DataTable();
string strConn = "";
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'"; using (OleDbConnection connection = new OleDbConnection(strConn))
{
connection.Open(); // 取第一个SheetName #region SheetName
DataTable schemaTable = connection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string sheetName = schemaTable.Rows[0][2].ToString().Trim();
sheetName = FormatSheetName(sheetName);
#endregion
// 数据
string strSqlSelect = "";
strSqlSelect = "SELECT * FROM [" + sheetName + "] ";
OleDbCommand command = new OleDbCommand(strSqlSelect, connection);
OleDbDataAdapter adapt = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
adapt.Fill(ds);
}