码迷,mamicode.com
首页 > Web开发 > 详细

asp.net利用剪切板导出excel

时间:2014-12-31 09:50:38      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

  

asp.net利用剪切板导出excel

标签:

原文地址:http://www.cnblogs.com/xiaofengju/p/4195047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!