DATAGRID 导出EXCEL的方法
许多朋友,在做DATAGRID中的数据导成EXCEL的时候,都碰到问题,需要些较多的代码,现在,大家可以用UltraWinGrid, UltraGridExcelExporter来实现EXCEL导出。
首先,分别拉一个UltraWinGrid, UltraGridExcelExporte控件到项目中,配置好UltraWinGrid数据源[这里省略了],然后就是导出数据:
//新建一个工作簿
Workbook myWorkbook = new Workbook();
Worksheet myWorksheep = myWorkbook.Worksheets.Add("新建工作表");
//设置横向打印
myWorksheep.PrintOptions.Orientation = Infragistics.Excel.Orientation.Landscape;
//设置页眉
myWorksheep.PrintOptions.Header = "&L &/r/n 表Ⅰ" +"&C&13报表页眉" +"&R&/r/n单位:亩 第&P页共&N页";
//设置页脚
myWorksheep.PrintOptions.Footer = "&L填表人:xxx 填表日期&D &R填表人:xxx 填表日期&D ";
//导出EXCEL
this.ExproterExel.Export(this.ultraGridmj,myWorksheep);
BIFF8Writer.WriteWorkbookToFile(myWorkbook, this.savef.FileName.ToString());
以下是有关UltraWinGrid导出EXCEL的一些技术文档
Exports the passed in ultragrid to the specified worksheet.
-----------------------------------------------------------------------------------------------------------------
Gets or sets the header for each page of the printed worksheet.
Visual Basic (Declaration) | |
---|---|
Public Property Header As String |
C# | |
---|---|
public string Header {get; set;} |
The '&' character in the header and footer is a special character. Depending on what is after it, the formatting of the text can be controlled or dynamic text can be inserted. Below is a list of the available commands:
Section commands | |
---|---|
&L | The following text will appear in the left section. The formatting of new sections is always the default formatting, regardless of the formatting of the previous section. |
&C | The following text will appear in the center section. The formatting of new sections is always the default formatting, regardless of the formatting of the previous section. |
&R | The following text will appear in the right section. The formatting of new sections is always the default formatting, regardless of the formatting of the previous section. |
Replacement commands | |
---|---|
&& | Insert the '&' character. |
&A | Insert the current worksheet name. |
&D | Insert the current date. |
&F | Insert the current file name. |
&G | Insert an image (Note: This command is currently not supported). |
&N | Insert the number of pages the worksheet will need to print. |
&P | Insert the current page number. |
&T | Insert the current time. |
&Z | Insert the current file path (without the file name). |
Formatting commands | |
---|---|
&B | Toggle bold. |
&E | Toggle double underlining. |
&I | Toggle italics. |
&S | Toggle strikethrough. |
&U | Toggle underlining. |
&X | Toggle superscript. |
&Y | Toggle subscript. |
&<FontSize> | Sets a new font size for the following text. The size is expressed as a positive integer. If a number is to follow this command in the header, it must be separated by a space. |
&"<FontName>" | Sets a new font for the following text. If the font name is not recognized, the default font will be used. |
&"<FontName>,<FontStyle>" | Sets the new font and font style for the following text. The font style is usually "Regular", "Bold", "Italic", or "Bold Italic", but can be other styles depending on the font. The available font styles can be seen in the font dialog when a font is selected. |
The header or footer string could look like this: &L&"Arial,Bold"&D&CPage &P of &N on &A&R&14&F.
Visual Basic | |
---|---|
Imports Infragistics.Excel |
C# | |
---|---|
using System.Windows.Forms; |
Visual Basic (Declaration) | |
---|---|
Public Overloads Function Export( _ ByVal grid As UltraGrid, _ ByVal worksheet As Worksheet _) As Workbook |
C# | |
---|---|
public Workbook Export( UltraGrid grid, Worksheet worksheet) |
C# | |
---|---|
private void button2_Click(objectsender, System.EventArgs e) |
Visual Basic | |
---|---|
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click |
转自:http://blog.csdn.net/guanzg/article/details/3876194