Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using System.IO;
Using Microsoft.Office.Interop.Excel;
Using System.Data.OleDb;
Namespace Text_one
{
public partial class Form1:form
{
public Form1 ()
{
initializecomponent ();
}
private void button1_click (object sender, EventArgs e)
&NBSP ; {
OpenFileDialog file = new OpenFileDialog ();
file. Filter = "Exce file (*,xls,xlsx) |*.xls;*.xlsx";
if (file. ShowDialog () = = DialogResult.OK)
{
&NBSP ; string FileName = file. FileName;
datagridview1.datasource = Getexceldata (FileName);// Bang
datagridview1.datamember = "[sheet$]";
Datagridviewtextboxcolumn Dtctimestamp = new Datagridviewtextboxcolumn ();
Dtctimestamp.datapropertyname = "TIMESTAMP"; the column name obtained by the//SQL statement, which can be obtained from the collection
Dtctimestamp.width = 110;
DATAGRIDVIEW1.COLUMNS.ADD (Dtctimestamp);//Must be added in the end
}
}
public static DataSet Getexceldata (String str)
{
String Strcon = "Provider=microsoft.ace.oledb.12.0;data source=" + str + "; Extended properties= ' Excel 12.0; Hdr=yes;imex=1, ' ";
OleDbConnection myconn = new OleDbConnection (Strcon);
String strcom = "SELECT * FROM [sheet$]";
MyConn.Open ();
OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, myconn);
DataSet myDataSet = new DataSet ();
Mycommand.fill (myDataSet, "[sheet$]");
Myconn.close ();
return mydataset;
}
private void Button2_Click (object sender, EventArgs e)
{
String a = "";
Exportexcels (A, dataGridView1);
}
private void Exportexcels (String fileName, DataGridView MYDGV)
{
String savefilename = "";
SaveFileDialog Savedialog = new SaveFileDialog ();
Savedialog.defaultext = "xls";
Savedialog.filter = "Excel file |*.xls";
Savedialog.filename = FileName;
Savedialog.showdialog ();
Savefilename = Savedialog.filename;
if (Savefilename.indexof (":") < 0) return; It was canceled.
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application ();
if (xlapp = = null)
{
MessageBox.Show ("Unable to create Excel object, may not have Excel installed on your machine");
Return
}
Microsoft.Office.Interop.Excel.Workbooks Workbooks = xlapp.workbooks;
Microsoft.Office.Interop.Excel.Workbook Workbook = workbooks. ADD (Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet Worksheet = (Microsoft.Office.Interop.Excel.Worksheet) workbook. worksheets[1];//get Sheet1
Write title
for (int i = 0; i < Mydgv.columncount; i++)
{
Worksheet. Cells[1, i + 1] = Mydgv.columns[i]. HeaderText;
}
Write value
for (int r = 0; r < MyDGV.Rows.Count; r++)
{
for (int i = 0; i < Mydgv.columncount; i++)
{
Worksheet. Cells[r + 2, i + 1] = Mydgv.rows[r]. Cells[i]. Value;
}
System.Windows.Forms.Application.DoEvents ();
}
Worksheet. Columns.EntireColumn.AutoFit ();//Column width adaptive
if (savefilename! = "")
{
Try
{
Workbook. Saved = true;
Workbook. SaveCopyAs (Savefilename);
}
catch (Exception ex)
{
MessageBox.Show ("An error occurred while exporting the file, the file may be being opened!") \ n "+ ex. Message);
}
}
xlApp.Quit ();
Gc. Collect ();//forcibly Destroy
MessageBox.Show ("File xls saved successfully", "information hint", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
About export and import of C #