ASP. NET exports Excel using Clipboard

Source: Internet
Author: User

public enum Clipboardformats:uint {cf_text = 1, Cf_bitmap = 2, cf_metafilepict = 3, cf_s YLK = 4, Cf_dif = 5, Cf_tiff = 6, Cf_oemtext = 7, Cf_dib = 8, Cf_palette = 9, CF  _pendata = ten, Cf_riff = one, Cf_wave = one, Cf_unicodetext =, Cf_enhmetafile =}public    Class clipboardutility{[DllImport ("User32.dll")] private static extern bool OpenClipboard (IntPtr Hwndnewowner);    [DllImport ("User32.dll")] private static extern bool EmptyClipboard ();    [DllImport ("User32.dll")] private static extern IntPtr GetClipboardData (uint Uformat);    [DllImport ("User32.dll")] private static extern IntPtr SetClipboardData (uint Uformat, IntPtr hmem);    [DllImport ("User32.dll")] private static extern bool CloseClipboard ();        [DllImport ("Kernel32.dll")] private static extern UIntPtr GlobalSize (IntPtr hmem); public static void Setclipboardtext (string text, Encoding Encoding) {byte[] bytes = encoding.        GetBytes (text); IntPtr alloc = Marshal.allochglobal (bytes.        Length + 1); Marshal.Copy (bytes, 0, alloc, bytes.        Length);        OpenClipboard (IntPtr.Zero);        EmptyClipboard ();        SetClipboardData (UINT) clipboardformats.cf_text, alloc);    CloseClipboard ();        } public static string Getclipboardtext (Encoding Encoding) {openclipboard (IntPtr.Zero);        IntPtr alloc = GetClipboardData ((UINT) clipboardformats.cf_text);        byte[] bytes = new byte[(int) globalsize (alloc)]; Marshal.Copy (alloc, bytes, 0, bytes.        Length);        CloseClipboard (); return encoding.    GetString (bytes);    } [STAThread] public static void Clear () {clipboard.clear ();            }} public bool Createexcelfilefordatatable () {Excel.Application app = new Excel.Application (); App. Visible = true;//lets Excel display (debug) excel.workbooks ws = App.            Workbooks; Excel.Workbook Workbook = ws. ADD (Excel.XlWBATemplate.xlWBATWorksheet); By default, a worksheet int sheetcount = 2;//excel page workbook is created. Sheets.add (Type.Missing, workbook.            Sheets[1], Sheetcount, Type.Missing);            list<stringbuilder> list = new list<stringbuilder> ();            StringBuilder sb = new StringBuilder (); Sb. Append (@ "<table align= ' center ' border= ' 0 ' cellpadding= ' 2 ' cellspacing= ' 0 ' width= ' 100% ' ><tbody><tr &GT;&LT;TD style= ' color: #ff0000 ' ><asp:label id= ' lblTX0 ' runat= ' Server ' >wt</asp:label><br/> &LT;/TD&GT;&LT;TD style= ' color: #ff0000 ' ><asp:label id= ' lblTX0 ' runat= ' Server ' >wt</asp:label><br /&GT;&LT;/TD&GT;&LT;/TR&GT;&LT;TR&GT;&LT;TD style= ' color: #ff0000 ' ><asp:label id= ' lblTX0 ' runat= ' Server ' > WT&LT;/ASP:LABEL&GT;&LT;BR/></td><td style= ' color: #ff0000 ' ><asp:label id= ' lblTX0 ' runat= ' Server ' >wt</asp:label><br/></td></tr></tbody></table> '); List.            ADD (SB); List.            ADD (SB); List.            ADD (SB); for (int i = 1; I <= workbook. Sheets.count; i++) {Excel.Worksheet Worksheet = (excel.worksheet) workbook.  Worksheets[i]; get worksheet worksheet. Name = "First" + i.tostring () + "sheet";//Set name Clipboardutility.setclipboardtext for worksheet (List[i-1]. ToString (), encoding.default);//Set Clipboard contents worksheet with default encoding.                Paste ();//paste from the Clipboard into Excel. Worksheet.  Columns.EntireColumn.AutoFit (); Auto Fit Length}//If Excel does not display, remember to close excel at last, otherwise it will open a lot in memory. Ws.        Close (); App.        Quit ();        System.Runtime.InteropServices.Marshal.ReleaseComObject (worksheet);        worksheet = null;        System.Runtime.InteropServices.Marshal.ReleaseComObject (workbook);        workbook = null;        System.Runtime.InteropServices.Marshal.ReleaseComObject (WS);        WS = NULL;  System.Runtime.InteropServices.Marshal.ReleaseComObject (APP);      App = Null;return true;} 

  

ASP. NET exports Excel using Clipboard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.