Excel. dll export Excel control 2932 people read comments (2) favorite reports excelmicrosoftstring product server Google
I recently made a program for exporting excel! That's annoying.
The requirement is to directly export the gridview to excel, which is simple!
This will solve the problem! However, you must rewrite the method to export private void toexcel (control CTL, string filename)
{
Httpcontext. Current. response. charset = "UTF-8 ";
Httpcontext. Current. response. contentencoding = system. Text. encoding. utf8;
Httpcontext. Current. response. contenttype = "application/MS-excel ";
Httpcontext. Current. response. appendheader ("content-disposition", "attachment; filename =" + "" + filename );
CTL. Page. enableviewstate = false;
System. Io. stringwriter Tw = new system. Io. stringwriter ();
Htmltextwriter hW = new htmltextwriter (TW );
CTL. rendercontrol (HW );
Httpcontext. Current. response. Write (TW. tostring ());
Httpcontext. Current. response. End ();
} Public override void verifyrenderinginserverform (Control)
{
// Confirms that an htmlform control is rendered
} Later, I requested another change. adding something to the exported Excel file is not just the gridview Reed value! Tricky... then I thought of adding HTML code to ostringwriter to add a value. For example, table partitioning and control. For example, chanpdatagrid. allowpaging = false; // clear the page
Chanpdatagrid. allowsorting = false; // clear the sorting
Datatable dtchanp = (datatable) session ["sendchanpdt"];
Chanpdatagrid. Columns. removeat (5); // clear the operation Column
Chanpdatagrid. datasource = dtchanp;
Chanpdatagrid. databind (); response. Clear ();
Response. Buffer = true;
Response. charset = "gb2312 ";
Response. appendheader ("content-disposition", "attachment?filename=sendinfo.xls ");
Response. contentencoding = system. Text. encoding. utf7;
Response. contenttype = "application/MS-excel"; // set the output file type to an Excel file.
System. Io. stringwriter ostringwriter = new system. Io. stringwriter ();
Ostringwriter. Write ("Area:"); ostringwriter. Write (areaddlist. selecteditem. Text + "<br/> ");
Ostringwriter. write ("invoicing unit:"); ostringwriter. write (kai_dw.selecteditem.text + "& nbsp;"); ostringwriter. write ("Recipient:"); ostringwriter. write (shuodw. text + "<br/> ");
Ostringwriter. write ("Applicant:"); ostringwriter. write (reqdw. text + "& nbsp;"); ostringwriter. write ("requester name:"); ostringwriter. write (req_name.text + "<br/> ");
Ostringwriter. write ("place of delivery:"); ostringwriter. write ("from & nbsp;" + dzstart. text + "& nbsp; To & nbsp;" + dzend. text + "& nbsp;"); ostringwriter. write ("request delivery time:"); ostringwriter. write (req_senddate.text + "<br/> ");
Ostringwriter. write ("Contact:" + link_name.text + "& nbsp;"); ostringwriter. write ("Contact Phone:" + link_tel.text + "& nbsp;" + "Contact Fax:" + link_fax.text + "<br/> ");
Ostringwriter. Write ("sales company marketing management office:"); ostringwriter. Write (xsmana. Text + "<br/> ");
Ostringwriter. write ("shipping method:"); ostringwriter. write (send_type.selecteditem.text + "& nbsp;"); ostringwriter. write ("Transfer Time:"); ostringwriter. write (cd_date.text + "<br/>"); system. web. UI. htmltextwriter ohtmltextwriter = new system. web. UI. htmltextwriter (ostringwriter );
This. chanpdatagrid. rendercontrol (ohtmltextwriter );
String STR = ohtmltextwriter. tostring ();
Response. Output. Write (ostringwriter. tostring ());
Response. Flush ();
Response. End (); then compare sb. Haha ~ The requirement has changed. You need to display the print preview directly! This headache ~ Only excel. dll is used! First, complete the installation of Excel. To perform the following configuration. This dll is very powerful for Excel operations. There are methods in it! To use this item, you must first configure the Excel operation permission on the computer. Enter dcomcnfg in start --> run to set it. The operation permission of the COM component must be set on the server side. Type "dcomcnfg" in the command line to go to the COM component configuration page, select Microsoft Excel, click Properties, select Custom for all three single options, and add everyone to all permissions in the editing. Set "ID" as an interactive user and restart the server after saving the settings.
* ** If you do not set an interactive user, if the Excel process stays in the memory, you cannot end the process on Google! In this way, the B/S program can operate the local Excel file. Create zyp. dbutility. excelhelper Excel = new zyp. dbutility. excelhelper ();
Excel. createexcel ();
Excel. worksheet sheet = (Excel. worksheet) excel. myworkbook. worksheets [1]; sheet. Visible = excel. xlsheetvisibility. xlsheetvisible;
Excel. myexcel. Caption = "Shipping Information ";
Sheet. Activate (); this is to create a sheet. For the cell style: excel. setcolumnwidth (1, 1, 9); // number
Excel. setcolumnwidth (3, 3, 20); // Product Model
Excel. setcolumnwidth (2, 2, 12); // material code
Excel. setcolumnwidth (4, 4, 8); // Order Quantity
Excel. setcolumnwidth (6, 6, 15); // Product Name
Excel. setcolumnwidth (7, 7, 25); // special description
Excel. setcolumnwidth (5, 5, 8); // confirm the quantity
Excel. cellautowrap (5, 7, (Dt. Rows. Count + 12), 7); // set automatic line feed
Excel. cellsalignment (4, 1, 4, 7, zyp. dbutility. excelhelper. excelhalign. Center, zyp. dbutility. excelhelper. excelvalign. center); // The title is centered.
// Excel. cellsalignment (8, 3, 8, 3, zyp. dbutility. excelhelper. excelhalign. right side, zyp. dbutility. excelhelper. excelvalign. center); // The right of the total drama
// Excel. cellsalignment (8, 3, 8, 3, zyp. dbutility. excelhelper. excelhalign. right side, zyp. dbutility. excelhelper. excelvalign. center); // right of the total drama
Excel. cellsalignment (DT. rows. count + 8), 7, (DT. rows. count + 12), 7, zyp. dbutility. excelhelper. excelhalign. left, zyp. dbutility. excelhelper. excelvalign. center); // The time is left
Excel. fontcolor (4, 1, 4, 7, zyp. dbutility. excelhelper. colorindex. Mei Hong );
Excel. fontstyle (4, 1, 4, 7, true, false, zyp. dbutility. excelhelper. underlinestyle. Underline-free); // The list header is bold.
Excel. fontstyle (3, 2, 3, 2, true, false, zyp. dbutility. excelhelper. underlinestyle. Underline-free); // The three rows are bold.
Excel. fontstyle (2, 1, 2, 1, true, false, zyp. dbutility. excelhelper. underlinestyle. Underline-free); // The two rows are bold.
Excel. fontstyle (1, 1, 1, 7, true, false, zyp. dbutility. excelhelper. underlinestyle. Underline-free); // a bold line
Excel. fontstyle (DT. rows. count + 12, 1, DT. rows. count + 12, 7, true, false, zyp. dbutility. excelhelper. underlinestyle. no underline); // The supervisor signature is bold
Excel. fontstyle (DT. rows. count + 4, 1, DT. rows. count + 4, 7, true, false, zyp. dbutility. excelhelper. underlinestyle. no underline); // bold total
// Excel. cellsbackcolor (4, 1, 4, 7, zyp. dbutility. excelhelper. colorindex. White, zyp. dbutility. excelhelper. pattern. Horizontal); // background of the detail table Header
Excel. cellsunite (Dt. Rows. Count + 5), 2, (Dt. Rows. Count + 5), 7); // remarks merged
// Excel. cellsunite (4, 6, 4, 7); // the product name is assigned to sheet. cells [1, 4] = "Notice of delivery ";
Sheet. cells [2, 1] = "Production Department Marketing Management Room :";
Sheet. cells [3, 2] = "Please assist with shipping of the following items. ";
// Delivery details
Sheet. cells [4, 1] = "no ";
Sheet. cells [4, 2] = "material code ";
Sheet. cells [4, 3] = "Product Model ";
Sheet. cells [4, 4] = "order quantity ";
Sheet. cells [4, 5] = "Confirm quantity ";
Sheet. cells [4, 6] = "Product Name"; For details, see your requirements. // excel. myexcel. Visible = true;
Sheet. pagesetup. printgridlines = false; // remove the gridlines during printing.
// Sheet. printpreview (true); this is the final display print. Yes, the relative path must be used during file export. Otherwise, the file is loaded after the Excel file is exported (you cannot find the file.../../style.css. Use Excell in the project. when the DLL component is used to operate the Excel file, everything works normally during debugging in VS 2005, but the following error occurs when the DLL component is deployed on the server: system. unauthorizedaccessexception: failed to retrieve components whose CLSID is {00024500-0000-0000-c000-000000000046} in the com class factory because of the following error: 80070005.
In excelhelper. startexcel () Location D: dotnetworkspacewebsitesscorecardapp_codeexcelhelper.cs: Row 111
Location D: dotnetworkspacewebsitesscorecardapp_codeexcelhelper.cs: row number 101
In scorecard. Process (string country) Location D: dotnetworkspacewebsitesscorecardapp_codescorecard.cs: Row 109
Google also found some solutions, but they did not work properly. After some attempts, they found the following solutions. system. add <identity impersonate = "true" username = "James/Administrator" Password = "##################" to the web section "##################" />
Username format: {MACHINE name}/{user name} password is the Login Password
[Convert] excel. dll export Excel Control