using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using Microsoft.Office.Interop;
using System.Runtime.InteropServices;
using System.Threading;
public partial class CreatePicture : System.Web.UI.Page
{
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern int GetWindowThreadProcessId(IntPtr hwnd, out   int ID);           
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
       
        Thread cbThread = new Thread(new ThreadStart(CopyToClipboard));
        cbThread.TrySetApartmentState(ApartmentState.STA);
        cbThread.Start();
        cbThread.Join();   
    }    [STAThread]
    protected void CopyToClipboard() 
    {
        if (Request.QueryString["MainBillNo"] != null)
        {
            string filePath = "\\" + "\\192.168.1.118\\印刷厂升级\\UPLOAD\\";
            string MainBillNo = Request.QueryString["MainBillNo"].ToString();
            string file_Excel = filePath + MainBillNo + "\\" + MainBillNo + ".bs.xls";
            string file_gif = filePath + MainBillNo + "\\" + MainBillNo + ".gif";
            Microsoft.Office.Interop.Excel.Application app =
                    new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbooks wbs = app.Workbooks;            Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(file_Excel);            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
            ws.UsedRange.Select();
            object missing = System.Reflection.Missing.Value;
            ws.UsedRange.Copy(missing);
            IDataObject iData = Clipboard.GetDataObject();
            Bitmap bits = (Bitmap)iData.GetData(DataFormats.Bitmap);
            Bitmap myBitmap = new Bitmap(bits.Width, bits.Height);
            Graphics g = Graphics.FromImage(myBitmap);
            g.DrawImage(bits, 1, 1);
            myBitmap.Save(file_gif, System.Drawing.Imaging.ImageFormat.Gif);
            myBitmap.Dispose();           
            bits.Dispose();
            wbs.Close();           
            IntPtr t = new IntPtr(app.Hwnd);
            int k = 0;
            GetWindowThreadProcessId(t, out   k);
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
            p.Kill();
            Response.Write("OK");
        }
        else
            Response.Write("ERROR");
    }
}用iis作服务器就会一直报剪切板操作失败 
哪位大侠帮忙解决一下

解决方案 »

  1.   

    原因2有可能是一下2点:
    1:Excel打开问题
     object MissingValue = Type.Missing;
                Excel.Workbook wb = MyExcel.Workbooks.Open(file_Excel, MissingValue,
                    MissingValue, MissingValue, MissingValue,
                    MissingValue, MissingValue, MissingValue,
                    MissingValue, MissingValue, MissingValue,
                    MissingValue, MissingValue, MissingValue,
                    MissingValue);
    打开Excel操作 必须这么写2:
    2.1,Excel 中你要设置你要剪切的范围
    2.2,多余的位置不要剪切,否则Excel会报错误的
      

  2.   

    在cs中是程序是可以用的但bs中就会出现‘所请求的剪贴板操作失败’这个异常