You can also search for an excel report online. There are just a few ideas, such as using the server that installs excel software or plug-ins to generate directly or directly on the client (usually using excel software or plug-ins to generate directly in the browser ). If you leave the excel plug-in, you can see that software companies, especially Microsoft, are powerful. Paste a simple method to export an excel report below. On the machine where office2003 is installed, you can use the IE browser to successfully generate an excel file, which has been used all the time. If you find this is not usable during the test, please note that this may be related to your machine configuration and do not doubt the correctness of the Code. The following is an example of iBatis development.
1. entity class
Copy to ClipboardReference: [www.bkjia.com] using System;
Using System. Collections. Generic;
Using System. Text;
# Region Apache Notice
/*************************************** **************************************
* $ Header: $
* $ Revision: 383115 $
* $ Date: March 20 13:21:51 + 0800 (Saturday, 04 August 06,) $
*
* IBatisNetDemo
* Copyright (C) 2006-Shanyou Zhang
*
* Licensed under the Apache License, Version 2.0 (the "License ");
* You may not use this file before t in compliance with the License.
* You may obtain a copy of the License
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the License is distributed on an "as is" BASIS,
* Without warranties or conditions of any kind, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*
**************************************** ****************************************/
# Endregion
Namespace IBatisNetDemo. Domain
{
[Serializable]
Public class Person
{
Private int id;
Private string firstName;
Private string lastName;
Private DateTime? BirthDate;
Private double? WeightInKilograms;
Private double? HeightInMeters;
Public Person (){}
Public int Id
{
Get {return id ;}
Set {id = value ;}
}
Public string FirstName
{
Get {return firstName ;}
Set {firstName = value ;}
}
Public string LastName
{
Get {return lastName ;}
Set {lastName = value ;}
}
Public DateTime? BirthDate
{
Get {return birthDate ;}
Set {birthDate = value ;}
}
Public double? WeightInKilograms
{
Get {return weightInKilograms ;}
Set {weightInKilograms = value ;}
}
Public double? HeightInMeters
{
Get {return heightInMeters ;}
Set {heightInMeters = value ;}
}
}
}
2. How to export the excel report main program
Copy to ClipboardReference: [www.bkjia.com] using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Web;
Using System. Web. UI;
Using System. IO;
Using System. Reflection;
Namespace DotNet. Common. Util
{
/// <Summary>
/// Simple excel export implementation
/// </Summary>
Public static class ExcelUtil
{
Private static Page currentPage = HttpContext. Current. Handler as Page;
Private static Object sycObj = new Object ();
Private static int incremental = 10;
/// <Summary>
/// Generate an excel name by time to prevent file overwrites caused by an excel file with the same name.
/// </Summary>
/// <Returns> </returns>
Private static string CreateExcelName ()
{
Lock (sycObj)
{
Incremental = incremental + 1;
If (incremental> 99)
Incremental = 10;
Return Convert. ToInt64 (DateTime. Now. ToString ("yyyyMMddHHmmssfff") + incremental). ToString ();
}
}
/// <Summary>
/// Export excel
/// </Summary>
/// <Typeparam name = "T"> generic Entity </typeparam>
/// <Param name = "response"> </param>
/// <Param name = "listColumes"> name of the column to be displayed </param>
/// <Param name = "listProperty"> the exported attribute name to be displayed depends on the attribute name of the object. The sequence is determined by the displayed column, which can be the same as that of listColumes. </param>
/// <Param name = "listModel"> object set </param>
Public static void ExportExcel <T> (HttpResponse response, IList <string> listColumns, IList <string> listProperty, IList <T> listModel) where T: class, new ()
{
If (listColumns. Count = 0)
{
Throw new IndexOutOfRangeException ("No Columnes! ");
}
If (listColumns. Count! = ListProperty. Count)
{
Throw new ArgumentException ("Columns and properties length are not equal .");
}
String sheetName = "sheetName ";
Using (StringWriter writer = new StringWriter ())
{
Writer. WriteLine ("Writer. WriteLine ("Writer. WriteLine ("<! -- [If gte mso 9]> ");
Writer. WriteLine ("<xml> ");
Writer. WriteLine ("<x: ExcelWorkbook> ");
Writer. WriteLine ("<x: ExcelWorksheets> ");
Writer. WriteLine ("<x: ExcelWorksheet> ");
Writer. WriteLine ("<x: Name>" + sheetName + "</x: Name> ");
Writer. WriteLine ("<x: WorksheetOptions> ");
Writer. WriteLine ("<x: Print> ");
Writer. WriteLine ("<x: ValidPrinterInfo/> ");
Writer. WriteLine ("</x: Print> ");
Writer. WriteLine ("</x: WorksheetOptions> ");
Writer. WriteLine ("</x: ExcelWorksheet> ");
Writer. WriteLine ("</x: ExcelWorksheets> ");
Writer. WriteLine ("</x: ExcelWorkbook> ");
Writer. WriteLine ("</xml> ");
Writer. WriteLine ("<! [Endif] --> ");
Writer. WriteLine ("Writer. WriteLine ("<body> ");
Writer. WriteLine ("<table> ");
Writer. WriteLine ("<tr> ");
Foreach (string item in listColumns)
{
Writer. WriteLine ("<td>" + item + "</td>"); // column name
}
Writer. WriteLine ("</tr> ");
// Display the columns to be displayed through reflection
BindingFlags bf = BindingFlags. Instance | BindingFlags. NonPublic | BindingFlags. Public | BindingFlags. Static; // reflection ID
Type objType = typeof (T );
PropertyInfo [] propInfoArr = objType. GetProperties (bf );
Foreach (T model in listModel)
{
Writer. WriteLine ("<tr> ");
Foreach (PropertyInfo propInfo in propInfoArr)
{
Foreach (string propName in listProperty)
{
If (string. Compare (propInfo. Name. ToUpper (), propName. ToUpper () = 0)
{
PropertyInfo modelProperty = model. GetType (). GetProperty (propName );
If (modelProperty! = Null)
{
Object objResult = modelProperty. GetValue (model, null );
Writer. WriteLine ("<td>" + (objResult = null )? String. Empty: objResult) + "</td> ");
}
Else
{
Throw new Exception ("Property name may not exists! ");
}
}
}
}
Writer. WriteLine ("</tr> ");
}
Writer. WriteLine ("</table> ");
Writer. WriteLine ("</body> ");
Writer. WriteLine ("Writer. Close ();
Response. Clear ();
Response. Buffer = true;
Response. Charset = "UTF-8 ";
CurrentPage. EnableViewState = false;
Response. AddHeader ("Content-Disposition", "attachment; filename =" + CreateExcelName () + ". xls ");
Response. ContentType = "application/ms-excel ";
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Response. Write (writer );
Response. End ();
}
}
}
}
3. web layer calls
Create a page named ExportExcelTest. aspx and add the test code to the page load. The specific class file is as follows:
Copy to ClipboardReference: [www.bkjia.com] using System;
Using System. Data;
Using System. Collections;
Using System. Collections. Generic;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using DotNet. Common. Util;
Using IBatisNetDemo;
Using IBatisNetDemo. Domain;
Public partial class ExportExcelTest: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
List <string> listColumns = new List <string> ();
ListColumns. Add ("ID ");
ListColumns. Add ("surname ");
ListColumns. Add ("name ");
ListColumns. Add ("Birthday ");
List <string> listProperties = new List <string> ();
ListProperties. Add ("Id ");
ListProperties. Add ("FirstName ");
ListProperties. Add ("LastName ");
ListProperties. Add ("BirthDate ");
List <Person> listModels = new List <Person> ();
Person person = new Person ();
Person. Id = 1;
Person. FirstName = "Wong ";
Person. LastName = "Jeff ";
Person. BirthDate = DateTime. Now. AddYears (-26 );
ListModels. Add (person );
Person = new Person ();
Person. Id = 2;
Person. FirstName = "Zhao ";
Person. LastName = "Jeffery ";
ListModels. Add (person );
// Export excel
ExcelUtil. ExportExcel <Person> (Response, listColumns, listProperties, listModels );
}
}
}
Now, write it here. In fact, every company has its own mature internal excel processing method, which is very simple and straightforward. If you think you can use it for your own purposes, do not forget the merits of my organization.