About export and import of C #

Source: Internet
Author: User

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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.