且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Excel文件转换为C#Windows应用程序表单

更新时间:2023-02-10 23:34:06

尝试

http://code.msdn.microsoft.com/office/Imoprt-Data-from-Excel-to-705ecfcd [ ^ ]

c#导入和导出代码excel到sql server [ ^ ]


为了查看我们为tis上传的excel文件的内容,我们应该遵循以下代码:

{

OleDbConnection theConnection = new OleDb连接(provider = Microsoft.Jet.OLEDB.4.0; data source = books.xls; Extended Properties = \Excel 8.0; HDR = NO; IMEX = 1; \);

theConnection.Open();

OleDbDataAdapter theDataAdapter = new OleDbDataAdapter(SELECT * FROM [Sheet1
For Viewing the content of the excel file which we uploaded for tis we should follow the below code:
{
OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=books.xls;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"");
theConnection.Open();
OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1


,theConnection);

DataSet theDS = new DataSet();

DataTable dt = new DataTable();

theDataAdapter.Fill(dt);

this。 dataGridView1.DataSource = dt.DefaultView;

}



要将Excel文件上传到C#windows应用程序表单,我们应该遵循以下代码:

{

OpenFileDialog fDialog = new OpenFileDialog();

fDialog.Title =选择要上传的文件;

fDialog.Filter =(* .xls)| * .xls;

if(fDialog.ShowDialog()== DialogResult.OK)

{

txtFileName.Text = fDialog.FileName.ToString();

}
", theConnection);
DataSet theDS = new DataSet();
DataTable dt = new DataTable();
theDataAdapter.Fill(dt);
this.dataGridView1.DataSource = dt.DefaultView;
}

For uploading a Excel file into C# windows application form we should follow the below code:
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Select file to be upload";
fDialog.Filter = "(*.xls)|*.xls";
if (fDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = fDialog.FileName.ToString();
}