Export word:
Private void button13_click (Object sender, system. eventargs E)
{
This. Maid = true;
Response. Clear ();
Response. Buffer = true;
Response. charset = "gb2312 ";
Response. appendheader ("content-disposition", "attachment?filename=file1.doc ");
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Response. contenttype = "application/MS-word ";
This. datagrid4.enableviewstate = false;
System. Io. stringwriter ostringwriter = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter ohtmltextwriter = new system. Web. UI. htmltextwriter (ostringwriter );
This. datagrid4.rendercontrol (ohtmltextwriter );
Response. Write (ostringwriter. tostring ());
Response. End ();
}
Export Excel:
Private void outexcel (DataGrid DG, string name, string type)
{DG. Visible = true;
Response. Clear ();
Response. Buffer = true;
Response. charset = "gb2312 ";
Response. appendheader ("content-disposition", name );
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Response. contenttype = type;
DG. enableviewstate = false;
System. Io. stringwriter ostringwriter = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter ohtmltextwriter = new system. Web. UI. htmltextwriter (ostringwriter );
DG. rendercontrol (ohtmltextwriter );
Response. Write (ostringwriter. tostring ());
Response. End ();
}
C #. Net: how to save the data of the DataGrid in Excel.
This is a class I wrote. You can directly place a DataSet object in the specified position of Excel, and you need to reference InterOP. Excel. dll
Using system;
Using system. Data;
Using system. xml;
Using Microsoft. VisualBasic;
Using Excel;
Using system. reflection;
Using system. IO;
Namespace soft1000.libs. Excel
{
Public class exceleditor
{
Private application app;
Private string mfilename;
Private workbook WB;
Private workbooks WBS;
// Private worksheet ws;
/// <Summary>
/// Open an Excel file object
/// </Summary>
/// <Param name = "FILENAME"> file name, including the disk path </param>
Public void open (string filename)
{
APP = new excel. applicationclass ();
WBS = This. App. workbooks;
WB = This. WBS. open (filename, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value );
Mfilename = filename;
}
/// <Summary>
/// Close the Excel operation object
/// </Summary>
Public void close ()
{
WB. Saved = true;
WB. Close (missing. Value, missing. Value, missing. value );
WBS. Close ();
App. Quit ();
WB = NULL;
WBS = NULL;
APP = NULL;
GC. Collect ();
}
/// <Summary>
/// Create an Excel operation object
/// </Summary>
Public void creat ()
{
APP = new applicationclass ();
WBS = app. workbooks;
WB = WBS. Add (true );
}
/// <Summary>
/// Save the Excel file object
/// </Summary>
/// <Returns> </returns>
Public bool save ()
{
Bool flag1;
Try
{
WB. Save ();
Flag1 = true;
}
Catch
{
Flag1 = false;
}
Return flag1;
}
/// <Summary>
/// The Excel file object is in another village
/// </Summary>
/// <Param name = "FILENAME"> </param>
/// <Returns> </returns>
Public bool saveas (Object filename)
{
Bool flag1;
Try
{
This. WB. saveas (filename, missing. value, missing. value, missing. value, missing. value, missing. value, xlsaveasaccessmode. xlnochange, missing. value, missing. value, missing. value, missing. value, missing. value );
Flag1 = true;
}
Catch
{
Flag1 = false;
}
Return flag1;
}
/// <Summary>
/// Rename Sheet
/// </Summary>
/// <Param name = "sheet"> worksheet object </param>
/// <Param name = "newsheetname"> New name </param>
/// <Returns> worksheet object </returns>
Public worksheet renamesheet (worksheet sheet, string newsheetname)
{
Sheet. Name = newsheetname;
Return sheet;
}
/// <Summary>
/// Rename shee
/// </Summary>
/// <Param name = "oldsheetname"> old name </param>
/// <Param name = "newsheetname"> New name </param>
/// <Returns> worksheet object </returns>
Public worksheet renamesheet (string oldsheetname, string newsheetname)
{
Worksheet worksheet2 = (worksheet) This. WB. worksheets [oldsheetname];
Worksheet2.name = newsheetname;
Return worksheet2;
}
/// <Summary>
/// Add a new worksheet
/// </Summary>
/// <Param name = "sheetname"> name </param>
/// <Returns> worksheet object </returns>
Public worksheet addsheet (string sheetname)
{
Worksheet worksheet2 = (worksheet) This. WB. worksheets. Add (missing. Value, missing. value );
Worksheet2.name = sheetname;
Return worksheet2;
}
/// <Summary>
/// Delete a worksheet
/// </Summary>
/// <Param name = "sheetname"> name of worksheets </param>
Public void delsheet (string sheetname)
{
(Worksheet) WB. worksheets [sheetname]). Delete ();
}
/// <Summary>
/// Obtain a worksheet object
/// </Summary>
/// <Param name = "sheetname"> worksheet name </param>
/// <Returns> worksheet object </returns>
Public worksheet getsheet (string sheetname)
{
Return (worksheet) This. WB. worksheets [sheetname];
}
/// <Summary>
/// Add a data table object to excel
/// </Summary>
/// <Param name = "DT"> datatable object </param>
/// <Param name = "ws"> worksheet object </param>
/// <Param name = "startx"> Start abscissa </param>
/// <Param name = "starty"> Start ordinate </param>
Public void addtable (system. Data. datatable DT, Worksheet WS, int startx, int starty)
{
Int num4 = DT. Rows. Count-1;
For (INT num1 = 0; num1 <= num4; num1 ++)
{
Int num3 = DT. Columns. Count-1;
For (INT num2 = 0; num2 <= num3; num2 ++)
{
WS. cells [num1 + startx, num2 + starty] = DT. Rows [num1] [num2]. tostring ();
}
}
}
/// <Summary>
/// Add a data table object to excel
/// </Summary>
/// <Param name = "DT"> datatable object </param>
/// <Param name = "ws"> worksheet name </param>
/// <Param name = "startx"> Start abscissa </param>
/// <Param name = "starty"> Start ordinate </param>
Public void addtable (system. Data. datatable DT, string WS, int startx, int starty)
{
Int num4 = DT. Rows. Count-1;
For (INT num1 = 0; num1 <= num4; num1 ++)
{
Int num3 = DT. Columns. Count-1;
For (INT num2 = 0; num2 <= num3; num2 ++)
{
This. getsheet (WS). cells [num1 + startx, num2 + starty] = DT. Rows [num1] [num2]. tostring ();
}
}
}
/// <Summary>
/// Set the cell value
/// </Summary>
/// <Param name = "ws"> worksheet object </param>
/// <Param name = "X"> cell abscissa </param>
/// <Param name = "Y"> cell ordinate </param>
/// <Param name = "value"> input value </param>
Public void setcellvalue (worksheet WS, int X, int y, object values)
{
WS. cells [x, y] = values. tostring ();
}
/// <Summary>
/// Set the cell value
/// </Summary>
/// <Param name = "ws"> worksheet name </param>
/// <Param name = "X"> cell abscissa </param>
/// <Param name = "Y"> cell ordinate </param>
/// <Param name = "value"> input value </param>
Public void setcellvalue (string WS, int X, int y, object values)
{
This. getsheet (WS). cells [x, y] = values. tostring ();
}
}
}
Save grid data in Excel
Public void exporttoexcel (string filename, system. Data. datatable DT, string excelname)
{
If (Dt = NULL) return;
String savefilename = "";
Bool filesaved = false;
Savefiledialog savedialog = new savefiledialog ();
Savedialog. defaultext = "xls ";
Savedialog. Filter = "Excel file | *. xls ";
Savedialog. filename = filename;
Savedialog. showdialog ();
Savefilename = savedialog. filename;
If (savefilename. indexof (":") <0) return; // canceled
Excel. Application xlapp = new excel. Application ();
If (xlapp = NULL)
{
MessageBox. Show ("an Excel object cannot be created, maybe your computer has not installed excel ");
Return;
}
Excel. workbooks = xlapp. workbooks;
Excel. Workbook workbook = workbooks. Add (Excel. xlwbatemplate. xlwbatworksheet );
Excel. worksheet = (Excel. worksheet) Workbook. worksheets [1]; // get sheet1
Excel. Range range;
Long totalcount = DT. Rows. count;
Long rowread = 0;
Float percent = 0;
Worksheet. cells [1, 1] = excelname;
// Write Fields
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Worksheet. cells [2, I + 1] = DT. Columns [I]. columnname;
Range = (Excel. Range) worksheet. cells [2, I + 1];
Range. Interior. colorindex = 15;
Range. Font. Bold = true;
}
// Write Value
// This. captionvisible = true;
For (INT r = 0; r <DT. Rows. Count; r ++)
{
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Worksheet. cells [R + 3, I + 1] = DT. Rows [r] [I];
}
Rowread ++;
Percent = (float) (100 * rowread)/totalcount;
// This. captiontext = "exporting data [" + percent. tostring ("0.00") + "%]...";
System. Windows. Forms. application. doevents ();
}
// This. captionvisible = false;
// This. captiontext = oldcaption;
Range = worksheet. get_range (worksheet. cells [2, 1], Worksheet. cells [DT. Rows. Count + 2, DT. Columns. Count]);
Range. borderaround (Excel. xllinestyle. xlcontinuous, Excel. xlborderweight. xlthin, Excel. xlcolorindex. xlcolorindexautomatic, null );
Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. colorindex = excel. xlcolorindex. xlcolorindexautomatic;
Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. linestyle = excel. xllinestyle. xlcontinuous;
Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. Weight = excel. xlborderweight. xlthin;
If (Dt. Columns. Count> 1)
{
Range. Borders [Excel. xlbordersindex. xlinsidevertical]. colorindex = excel. xlcolorindex. xlcolorindexautomatic;
Range. Borders [Excel. xlbordersindex. xlinsidevertical]. linestyle = excel. xllinestyle. xlcontinuous;
Range. Borders [Excel. xlbordersindex. xlinsidevertical]. Weight = excel. xlborderweight. xlthin;
}
If (savefilename! = "")
{
Try
{
Workbook. Saved = true;
Workbook. savecopyas (savefilename );
Filesaved = true;
}
Catch (exception ex)
{
Filesaved = false;
MessageBox. Show ("An error occurred while exporting the file. The file may be opened! \ N "+ ex. Message );
}
}
Else
{
Filesaved = false;
}
Xlapp. Quit ();
GC. Collect (); // forcibly destroy
If (filesaved & file. exists (savefilename) system. Diagnostics. process. Start (savefilename );
}
}