Sometimes we need to export the database data to an Excel table for viewing and analysis. How can we export it? The following code is used for implementation.First, create a new project and add reference Microsoft. Office. Interop. Excel. dll. Take the Oracle database as an example (you only need to read the able or DataSet
must change the User ID ' Create a connection and open it.
Dim objconn as New System.Data.SqlClient.SqlConnection ("User id=objConn.Open ()
Dim strSQL as String
Dim Objdataset as New DataSet ()
Dim Objadapter as New System.Data.SqlClient.SqlDataAdapter ()
The customers from the USA.
strSQL = "SELECT * FROM Customers where country= ' USA '"
Objadapter.selectcommand = New System.Data.SqlClient.SqlCommand (strSQL, objconn)
' Fill the
When I imported Excel Data yesterday, I accidentally found that some of the data could not be imported normally. Later I found that the data item content was already null when I imported it to dataset. It seems that it was an Excel
") + "; User id=admin; password=; ";OleDbConnection ODBC = new OleDbConnection (strconn);return ODBC;}protected void Button1_Click (object sender, EventArgs e){Defining Excel ListsString StyleSheet = "Sheet1";Call the custom LoadData method to read the data in the Excel file to the ASPNET pageLoadData (StyleSheet);To define the SQL statement for a queryString sql
uploaded file content on the server
Dataset DS
=
Excelsqlconnection (savepath, filename );
//
Call a custom Method
Datarow [] Dr
=
DS. Tables [
0
]. Select ();
//
Define a datarow Array
Int
Rowsnum
=
DS. Tables [
0
]. Rows. count;
If
(Rowsnum
=
0
){Response. Write (
"
"
);
//
Prompt the user when the Excel table is empty
}
Else
I've been very busy lately and haven't written a blog for a long time. Today, let's explain how to import Excel data into Excel in C #, and at the end of the article, how to import data using SQL Server and MySQL tools.The import process is broadly divided into two steps:
1. Import the
the code to webform1.aspx. CSPage_loadEvent:// Create connection string variable. Modify the "Data Source"// parameter as appropriate for your environment.String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +"Extended Properties=Excel 8.0;";// Create connection object by using the preceding co
properties = 'excel 12.0 XML; HDR = Yes '";// SQL statement// String strexcel = string. Format ("select * from [{0} $]", strsheetname); this is a methodString strexcel = "select * from [" + strsheetname + "]";// Define the stored data tableDataset DS = new dataset ();// Connect to the data sourceUsing (oledbconnection
adjustments.
We need to transform the data and convert this series of numbers into DataFrame so that it can be more easily merged into existing data. The T function allows us to change the data arranged by row to the data arranged by column.
df_sum=pd.DataFrame(data=sum_row
C # operation Excel data additions and deletions to check.
First create the Exceldb.xlsx file and add two sheets.
Work Table 1:
UserInfo tables, fields: UserId, UserName, age, address, createtime.
Work Table 2:
Order table, Fields: OrderNo, ProductName, Quantity, Money, saledate.
1, create ExcelHelper.cs class, Excel File Processing class
Copy Code c
("UserScore. mdb") + "; User Id = admin; Password = ;";OleDbConnection odbc = new OleDbConnection (strconn );Return odbc;}Protected void button#click (object sender, EventArgs e){// Define the Excel listString StyleSheet = "Sheet1 ";// Call the custom LoadData method to read data from the Excel file to the ASPNET pageLoadData (StyleSheet );// Define the query SQ
; IEnumerable // DateTime. fromOADate (double. parse (value); if it is determined to be a date, you can directly use this method to convert it to a date object, but you cannot determine whether the CELL data is floating or date when DataType = NULL. (date is automatically converted to floating point return value ;}}}}
How to import an Excel file to data by readi
OleDbDataAdapter (SQL,CONNSTR);DataSet ds=new DataSet ();Da. Fill (DS);Datagrid1.datasource=ds;Datagrid1.databind ();Conn.close ();However, the result of running the above code is not expected, it will appear as shown in table 2. You can find that the two data items in the first field that are "1042" become empty.Table 2 The
(Rowsnum = = 0){Response.Write (" "); Prompt the user when the Excel table is empty}Else{for (int i = 0; i {In front of you need to create a "upfiles" folder, the others do not have to control, you just need to get the value of Excel in the following way, and then put these values in your way to insert into the databasestring title = dr[i]["caption"]. ToString ();String linkurl = dr[i]["link Address"]. ToS
.
Step 2: Create a New winfrom form application named exportorimport. Put a dview control and a button control on the form.
Step 2: Create a new Lib folder in the current project, copy the following class libraries to the folder, right-click "Reference" → "add reference", and find the DLL files of Lib, complete adding.
Step 3: Introduce the following namespace in the project:
Using npoi. hssf. usermodel;
Using npoi. hpsf;
Using npoi;
Using system. IO;
Step 4: read
}
Else
{
for(inti =0; I )
{
//In front of you need to create a "upfiles" folder, the others do not have to control, you just need to get the value of Excel in the following way, and then put these values in your way to insert into the database
stringtitle = dr[i]["title"].
ToString (); stringLinkurl = dr[i]["Link Address"].
ToString (); st
When querying Excel data, if the values in a column contain numbers and texts, some text values may not be read from the data obtained using the query statement. for this reason, I want to find a large segment on the Internet.
In fact, it is very easy to solve this problem, as long as the connection string instructs all columns to be processed by text, so that
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.