In this method, all the sheet data in the input Excel file is filled in the dataset. This is a simple and quick method. The disadvantage is that it is not suitable for complicated Excel files.
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
Using
System.
Protected void exportexcel_bn_click (Object sender, eventargs E){Fileinfo file = new fileinfo (server. mappath ("_ resourses \ upexcel \ studentmodel.xls"); // Excel file template
String SQL = "select XH as student ID, XM as name, XB as gender, yxdm as faculty code, zydm as professional code, bjdm as class code, xjzt as student status, csny as date of birth, Syd as source, jgm as nationality, sfzh as ID number, MZ as ethnic code, zzmm as political app
This is a very simple technical point. If someone asks, I will send it to you:
Public
Dataset getexcel (
String
Path)
{ // Directoryinfo parsedirect = new system. Io. directoryinfo (PATH ); // Fileinfo [] xlsfile = parsedirect. getfiles ("*. xls "); Oledbconnection objconn = Null ;Dataset data = New D
1. First, generate a table for link operations. If the two tables are in the same database, I want to use SQL statements directly. There is no need to obtain the local database before link operations; if the two files are not in the same database (for example, the two files in Excel are equivalent to the two databases), you have to extract the data tables you are interested in from each database first, put
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
);Add the following empty Method to the page.Public override void VerifyRenderingInServerForm (Control control Control){} 3. export data from DataSet to ExcelWith the above idea, we can export the exported information to the Response client. Export the data in the DataSet, that is, to export the information of each ro
(datagrid1 );Add the following empty Method to the page.Public override void VerifyRenderingInServerForm (Control control Control){}
3. export data from DataSet to ExcelWith the above idea, we can export the exported information to the Response client. Export the data in the DataSet, that is, to export the information
ProjectIntroductionLightweight and fastOfC #Compiled libraryRead from Microsoft Excel file ('97-2007).Cross-platform:
-Windows with. NET Framework 2-Windows Mobile with Compact framework-Linux, OS X, BSD with mono 2 + How to Use
C #Code:
Filestream stream = File. Open (filepath, filemode. Open, fileaccess. Read ); // 1. reading from a binary Excel file ('97-2003 format; *. xls) Iexceldatareader excelr
(datagrid1 );3. Export the data in DataSet to Execl
With the above idea, we can export the exported information to the Response client. Export the data in the DataSet, that is, to export the information of each row in the table in the DataSet in the ms-
Import data from excel to sqlserver, and import data from excel to sqlserver
1. Read excel Data to dataset
public static System.Data.DataSet ExcelSqlConnection(string filepath, string t
);
HttpContext. Current. Response. Write (tw. ToString ());
HttpContext. Current. Response. End ();
If your DataGrid uses paging, it exports the information of the current page, that is, it exports the information displayed in the DataGrid. Instead of all the information of your select statement.
For ease of use, the writing method is as follows:Public void DGToExcel (System. Web. UI. Control ctl)
{
HttpContext. Current. Response. AppendHeader ("Content-Disposition", "attachment?filename=excel.
* from [sheet1$]", conn);
OleDbDataAdapter ADP = new OleDbDataAdapter (CMD);
DataSet ds = new DataSet ();
Adp. Fill (DS);
Datagridview2.datasource = ds. Tables[0];
Traversing the contents of a schema[CSharp]View Plaincopy
DataTable DT = conn. GetSchema ();
for (int i = 0; i
{
TextBox1.Text + = dt. Columns[i]. Caption;
if (i + 1
{
TextBox1.Text + = ",";
}
}
());
HttpContext.Current.Response.End ();
}
Usage: dgtoexcel (DATAGRID1);
The following empty method needs to be added to the page.
public override void Verifyrenderinginserverform (Control control)
{
}
3. Export the data in the DataSet to Excel
With the above idea, is to be exported in the information, output (Response) client, so that it
(System.Exception ex) { TextBox1.Text + = ("Insert data failed:" + ex. Message); TextBox1.Text + = ("\ r \ n"); }
Display the contents of the table "Sheet1" in dataGridView2, you need to add the "$" symbol after the table name when accessing the Excel table
cmd = new OleDbCommand ("SELECT * from [sheet1$]", conn);
@@@@@@@@@// The following Code reads data from an Excel file and writes it to access.// Introduce EXCEL to create an Excel source data file and an access target data file on disk D
Public static datatable exceltodatatable (string
This paper illustrates some common methods of Excel data export in ASP.net, and also introduces some problems that may be encountered in data import or export, and share them for everyone's reference. I hope the article will help you. The implementation methods are as follows:
1, generated by the dataset
Copy
/* Extract data from the database */
String strconn = "packet size = 4096; user id = sa; Data Source = localhost; persist Security info = true; initial catalog = database; Password = sa ";
Sqlconnection sqlconn = new sqlconnection (strconn );
Sqlconn. open ();
Sqldataadapter = new sqldataadapter ("select * from [data table]", sqlconn );
1, generated by the dataset
The code is as follows
Copy Code
public void Createexcel (DataSet ds,string typeid,string FileName){HttpResponse resp;RESP = Page.Response;Resp. ContentEncoding = System.Text.Encoding.GetEncoding ("GB2312");Resp. Appendheader ("Content-disposition", "attachment;filename=" + filename);String Colheaders= "", ls_item= "";int i=0;Defines a Table object as
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.