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 ><TD style= ' color: #ff0000 ' ><asp:label id= ' lblTX0 ' runat= ' Server ' >wt</asp:label><br/> </TD><TD style= ' color: #ff0000 ' ><asp:label id= ' lblTX0 ' runat= ' Server ' >wt</asp:label><br /></TD></TR><TR><TD style= ' color: #ff0000 ' ><asp:label id= ' lblTX0 ' runat= ' Server ' > WT</ASP:LABEL><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