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 = 10, CF_RIFF = 11, CF_WAVE = 12, CF_UNICODETEXT = 13, CF_ENHMETAFILE = 14 } 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;//让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; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。