In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the selective export of Excel solutions from GridView?". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. demand analysis
First of all, according to the needs of users, selective export can be carried out. All the exports have been done before, and this new demand has come again, but it is right to think about it carefully. It is not meaningful to export all tens of thousands of pieces of data, but selectively exporting the data that users need is what users need. Where there is a need, there is a solution.
There is more than one solution to any problem, and so is the selective export of GridView. First of all, GridView binds the table data of the database, and we can choose to export. We can also query the required data from the data and obtain the data directly from the foreground GridView table. The question is, how to judge whether the primary key of the selected message (or the whole message) is selected? which direction is more efficient?
II. Solutions
ONE
1. The foreground obtains the primary key information of the selected row, transmits it to the background through JSON format, parses it in the background, and then obtains the data from the database and exports it to Excel through DataTable.
1. Export the selected action
The copy code is as follows:
two。 Foreground JS, the primary key that gets the information of the selected row, that is, GridView must have a column that is the primary key, get all the primary keys and save them in JSON format, and then transfer them to the server
The copy code is as follows:
Function getnumberExport () {
If (ExportCheck ()) {
/ / obtain elements through GridView ID
Var gv = document.getElementById ("ctl00_ContentPlaceHolder1_gridView")
/ / obtain the Input html of GridView
Var mycheck = gv.getElementsByTagName ("input")
/ / define a new array
Var fam = new Array ()
Var hg
Var id
/ / cycle through the checkbox tag to get the primary key information of each piece of data
For (var I = 0; I < mycheck.length; iTunes +) {
If (mycheck.type = = 'checkbox') / / hidden
{
/ / if checkbox is selected
If (mycheck[ I]. Accounts = = true) {
Var numid = new Object ()
Hg = gv.rows (I + 1) .cells (20) [xss_clean]
The / / substring () method is used to extract characters that are mediated between two specified subscripts.
/ / LastIndexOf () reverses the string, fetching the specified character from back to front
Hg = hg.substring (hg.lastIndexOf ("=") + 1, hg.lastIndexOf ("\"))
/ / get the primary key of a piece of data
Id = gv.rows (I + 1) .cells (1) [xss_clean]
Numid.number = hg
Numid.id = id
Fam.push (numid)
}
}
}
/ / $.ajaxSetup ({
/ / async: false / / set to synchronous request
/ /})
/ / convert the array to JSON type
Var nid = JSON.stringify (fam)
/ / post mode to transfer data to the server
$.post ("List.aspx", {Action: "action", numid: nid}, function (result) {
});
}
Else {
Return false
}
}
3. JSON is parsed in the background, converted to DataTable, and exported to Excel
The copy code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
/ / determine whether the server receives data correctly
If (Request.Params ["Action"]! = null)
{
/ / obtain the JSON of the foreground
String numid = Request.Params ["numid"] .ToString ()
Session ["numid"] = numid
}
}
/ / /
/ / selectively export EXCEL
/ / /
/ / /
/ / /
Protected void Export_Click (object sender, EventArgs e)
{
FamilyPlanningFacade familyPlanningFacade = new FamilyPlanningFacade (); / / instantiate the appearance layer of family planning
DataSet dsAllFamily = new DataSet ()
DataSet dsOneFamily = new DataSet ()
DsAllFamily = familyPlanningFacade.GetAllList (); / / get all the family planning information
DsAllFamily.Tables [0] .Rows.Clear ()
/ / Newtonsoft.Json and Newtonsoft.Json.Linq libraries are required for parsing JSON;
JArray numidget = (JArray) JsonConvert.DeserializeObject (Session ["numid"] .ToString ()
/ / iterate through the required value in JSON, query the database with this value to get a piece of data, and add the data to a new row
For (int I = 0; I < numidget.Count; iTunes +)
{
JObject numberget = (JObject) numidget [I]
String num = numberget ["number"] .ToString ()
If (num.Equals ("))
{
/ / get the key value of id
String id = numberget ["id"] .ToString ()
/ / enBasic.ID = id
/ / query a piece of data through the value of id (unique)
DsExport = famfacade.SelectExport (id)
/ / New Construction Bank
DataRow row = newDataTable.NewRow ()
/ / add data to the new bank
Row.ItemArray = dsExport.Tables [0] .rows [0] .ItemArray
/ / add this line to DataTable
NewDataTable.Rows.Add (row)
}
}
DataTable dtfamilyplanning = new DataTable (); / / create a data table for family planning information
Dtfamilyplanning = newDataTable.Tables [0]
/ / set table field
Dtfamilyplanning = dtfamilyplanning.DefaultView.ToTable (false, new string [] {"ID", "name", "sex"})
/ / modify the header information in order to make the exported Excel header Chinese characters
Dtfamilyplanning.Columns ["ID"] .ColumnName = "number"
Dtfamilyplanning.Columns ["name"] .ColumnName = "name"
Dtfamilyplanning.Columns ["sex"] .ColumnName = "gender"
/ /.... Other information in the table is no longer detailed.
/ / create an object of the DataTableToExcel class
DataTableToExcel dtToExcel = new DataTableToExcel ()
/ / Export data to Excel
DtToExcel.ToExcel (dtfamilyplanning)
}
4. DataTable is exported as an Excel method, which is a good way to stand on the shoulders of giants for my use.
The copy code is as follows:
Public void ToExcel (DataTable dt)
{
DataGrid dgExcel = new DataGrid ()
DgExcel.DataSource = dt
DgExcel.DataBind ()
HttpContext.Current.Response.Charset = "GB2312"
String fileName = HttpUtility.UrlEncode (Guid.NewGuid () .ToString (), System.Text.Encoding.UTF8)
String str = "attachment;filename=" + fileName + ".xls"
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8
HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.AppendHeader ("content-disposition", str)
StringWriter sw = new StringWriter ()
HtmlTextWriter htmTextWriter = new HtmlTextWriter (sw)
DgExcel.RenderControl (htmTextWriter)
HttpContext.Current.Response.Write ("")
String style = "td {mso-number-format:\"\ @\ ";}"; / / prevents all-digital data starting with 0 from being removed when excel is exported
HttpContext.Current.Response.Write (style)
HttpContext.Current.Response.Write ("")
HttpContext.Current.Response.Write (sw)
HttpContext.Current.Response.Write ("")
HttpContext.Current.Response.End ()
}
Finally, take a look at the whole implementation of this method:
1 obtain the primary key value of each selected row through the foreground
2 then save all the key values to the array, and convert the array to JSON format
3 is transmitted to the background through AJAX
4 the background receives, parses the JSON, and obtains the value in the JSON
5, you can query the database to get each selected data, and add each row of data to the DataTable
6 is finally exported to Excel.
The problems with this method are as follows: one. The foreground acquires the primary key value of the selected row of the GridView, and obtains the position of the primary key through innerHTML, that is, if the position of the primary key in the HTML changes, the code of the primary key will also change, which has a great impact on the whole body, which has a great impact on the flexibility of the system.
Let's look at the second method:
TWO
2. The background goes directly to the primary key collection of the GridView table in the foreground, and the implementation is similar to the previous method.
1. Export the selected action
The copy code is as follows:
two。 Background button event
The copy code is as follows:
Protected void Export_Click (object sender, EventArgs e)
{
DataSet dsAllFamily = new DataSet ()
DataSet dsOneFamily = new DataSet ()
FamilyPlanningFacade familyPlanningFacade = new FamilyPlanningFacade (); / / instantiate the appearance of family planning
DsAllFamily = familyPlanningFacade.GetAllList (); / / get all the family planning information
DsAllFamily.Tables [0] .Rows.Clear ()
/ / cycle through each line of gridView to find the selected row of CheckBox
Foreach (GridViewRow msgRow in this.gridView.Rows)
{
/ / get the CheckBox you need to traverse through ID
CheckBox chk = (CheckBox) msgRow.FindControl ("DeleteThis")
/ / determine whether CheckBox is selected
If (chk.Checked)
{
/ / get the key value of id
String id = msgRow.Cells [1] .Text.ToString ()
DsOneFamily = familyPlanningFacade.GetList (id)
/ / New Construction Bank
DataRow row = dsAllFaamily.Tables [0] .NewRow ()
/ / add data to the new bank
Row.ItemArray = dsOneFamily.Tables [0] .rows [0] .ItemArray
/ / add this line to DataTable
DsAllFamily.Tables [0] .Rows.Add (row)
}
}
If (dsAllFamily.Tables [0] .Rows.Count = = 0)
{
PersonalFiles.Web.MessageShow.MessageBox.Show (this, "Please select the information you want to export!")
Return
}
DataTable dtfamilyplanning = new DataTable (); / / create a data table for family planning information
Dtfamilyplanning = dsAllFamily.Tables [0]
/ / set table field
Dtfamilyplanning = dtfamilyplanning.DefaultView.ToTable (false, new string [] {"ID", "name", "sex"})
/ / modify the header information in order to make the exported Excel header Chinese characters
Dtfamilyplanning.Columns ["ID"] .ColumnName = "number"
Dtfamilyplanning.Columns ["name"] .ColumnName = "name"
Dtfamilyplanning.Columns ["sex"] .ColumnName = "gender"
/ /.... Other information in the table is no longer detailed.
/ / create an object of the DataTableToExcel class
DataTableToExcel dtToExcel = new DataTableToExcel ()
/ / Export data to Excel
DtToExcel.ToExcel (dtfamilyplanning)
}
The implementation process of this method is very simple. The primary key data displayed by the foreground GridView is obtained directly in the background, which is much simpler than the foreground JSON data.
Three
3. In addition to these two methods, there are faster ways to obtain the displayed data directly from the GridView instead of re-querying the database. The downside is that if the data is operated by many people and updated frequently, then obtaining the data directly from the interface GridView may not be the latest data.
The copy code is as follows:
Protected void Export_Click (object sender, EventArgs e)
{
DataTable dsExport = new DataTable ()
/ / cycle to get each column of gridView and get the header
For (int iTuno Bandi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.