首页 > 代码库 > asp.net利用剪切板导出excel
asp.net利用剪切板导出excel
public enum ClipboardFormats : uint { CF_TEXT = 1, CF_BITMAP = 2, CF_METAFILEPICT = 3, CF_SYLK = 4, CF_DIF = 5, CF_TIFF = 6, CF_OEMTEXT = 7, CF_DIB = 8, CF_PALETTE = 9, CF_PENDATA = http://www.mamicode.com/10,"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;//让Excel显示(调试用) Excel.Workbooks ws = app.Workbooks; Excel.Workbook workbook = ws.Add(Excel.XlWBATemplate.xlWBATWorksheet); // 默认已经创建了一个worksheet int sheetCount = 2;//Excel页数 workbook.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]; //得到worksheet worksheet.Name = "第" + i.ToString() + "个Sheet";// 为worksheet设置名字 ClipboardUtility.SetClipboardText(list[i - 1].ToString(),Encoding.Default);//用默认编码设置剪贴板内容 worksheet.Paste();//从剪贴板粘贴到Excel中。 worksheet.Columns.EntireColumn.AutoFit(); //自动适应长度 } //如果Excel不显示,记得最后要关闭Excel,不然会开很多在内存。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利用剪切板导出excel
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。