Read and Write excel Data and write excel Data

Source: Internet
Author: User

Read and Write excel Data and write excel Data

In actual projects, excel tables are inevitably operated. I have been reading excel tables all the time. But to write an excel table today, I am so painstaking that I have finally completed it and recorded it for later use.

 

1. read data from an excel table

Read the excel Data and import it to the database. According to common sense, as long as a dataset can be obtained, all the problems will be solved. The following figure shows the dataset obtained by reading the excel Data:

public DataSet ExecleDs(string filenameurl)        {            string strConn = "Provider=Microsoft.ACE.OleDb.12.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'";            OleDbConnection conn = new OleDbConnection(strConn);            conn.Open();            DataSet ds = new DataSet();            string strSql = string.Format("SELECT * FROM [{0}$]", "Sheet1");            OleDbDataAdapter odda = new OleDbDataAdapter(strSql, conn);            odda.Fill(ds, "hou");            return ds;        }

 

2. Write Data to an excel table

First, add two references:

Using System. Reflection; using Microsoft. Office. Interop. Excel;

 

1) initialization:

Object missing = Missing. value; Microsoft. office. interop. excel. application excel = new Microsoft. office. interop. excel. application (); // instantiate an excel Object Microsoft. office. interop. excel. workbook rsBook = excel. workbooks. open (fullFileName, missing, and missing); // Open the target file Microsoft. office. interop. excel. worksheet excelSheet = (Microsoft. office. interop. excel. worksheet) rsBook. sheets. get_Item (1); // set the first working thin excelSheet. activate (); // Activate the current workbook

 

2) write data:

for (int i = 1; i < 5; i++)                {                    excelSheet.Cells[i, 1] = i;                    excelSheet.Cells[i, 2] = i + 5;                    excelSheet.Cells[i, 3] = i + 10;                }

Note: The first row and first column of an excel table start with 1.

 

3) Save the excel file, Set Application attributes, and Recycle resources.

rsBook.Save();                excel.DisplayAlerts = false;                excel.Visible = true;                excelSheet = null;                rsBook = null;                excel = null;                GC.Collect();                GC.WaitForPendingFinalizers();                GC.Collect();                GC.WaitForPendingFinalizers();

How does vb read excel Data and store it in an array?

Read and Write EXCEL tables:
1. reference the Microsoft Excel Type Library in the project:

Select "Reference" from the "project" menu, select Microsoft Excel 12.0 Object Library (EXCEL2007), and then select "OK ". Indicates that the EXCEL Type Library is to be referenced in the project.

2. Define an EXCEL object during the declaration of a common object:

Dim xlApp As Excel. Application
Dim xlBook As Excel. WorkBook
Dim xlSheet As Excel. Worksheet

3. Common commands for operating EXCEL tables in a program:

Set xlApp = CreateObject ("Excel. Application") 'create an EXCEL Object
Set xlBook = xlApp. Workbooks. Open ("file name") 'Open an existing EXCEL Workbook File
Set xlBook = xlApp. Workbooks. Add ("file name") 'create an EXCEL worksheet File
XlApp. Visible = true' sets the EXCEL object to be Visible (or invisible)
Set xlSheet = xlBook. Worksheets ("table name") 'sets the activity Worksheet
For 1 to 100
For j = 1 to 50
NumArr (j, I) = xlSheet. cells (j, I) '......................................
Next
Next
XlBook. Close (True) 'Close the workbook
XlApp. Quit 'End the EXCEL Object
Set xlApp = Nothing 'to release the xlApp object
Zhang zhichen

How to use MATLAB to read data from EXCEL and write the data into EXCEL? It is best to return your thanks for an example!

Data = xlsread ('e: \ 123 ');
A = data (:, 1 );
B = data (:, 2 );
C = data (:, 3 );

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.