博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net利用剪切板导出excel
阅读量:5024 次
发布时间:2019-06-12

本文共 3547 字,大约阅读时间需要 11 分钟。

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
list = new List
(); StringBuilder sb = new StringBuilder(); sb.Append(@"
WT
WT
WT
WT
"); 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;}

  

转载于:https://www.cnblogs.com/xiaofengju/p/4195047.html

你可能感兴趣的文章
查看Linux信息
查看>>
Python中sys模块sys.argv取值并判断
查看>>
【详记MySql问题大全集】四、设置MySql大小写敏感(踩坑血泪史)
查看>>
并查集
查看>>
ubuntu 11.04下android开发环境的搭建!
查看>>
Bzoj 3343: 教主的魔法
查看>>
括号序列(栈)
查看>>
一件趣事
查看>>
DevExpress控件TExtLookupComboBox实现多列模糊匹配输入的方法
查看>>
atom 调用g++编译cpp文件
查看>>
H3C HDLC协议特点
查看>>
iptables 网址转译 (Network address translation,NAT)
查看>>
ios __block typeof 编译错误解决
查看>>
android 插件形式运行未安装apk
查看>>
ios开发之 manage the concurrency with NSOperation
查看>>
Android权限 uses-permission
查看>>
NSEnumerator用法小结
查看>>
vim如何配置go语言环境
查看>>
机器学习好网站
查看>>
python 中的 sys , os 模块用法总结
查看>>