就是一个自定义的接口,主要的目的就是不是打印一个文件。而是给你个某office产品的文件流。然后来实现这个打印功能 如何操作

解决方案 »

  1.   

    你必須寫做一個模板,列如EXCEL要打什麽樣,就先做一個模板,然後在頁面點擊某個控件把資料按照路徑都會進EXCEL表。   大概是不是這個意思啊?
      

  2.   

    public bool EXCELReport(string sTemplate, System.Data.DataTable ds, string sLanguage, System.Web.UI.Page page, bool bPrint, bool bExcel)
        {
            int a = 0;
            sTemplate = page.Server.MapPath(sTemplate);
            string sFile = page.Server.MapPath("~/App_Data/" + (a++) + ".xls");
            if (File.Exists(sFile)) File.Delete(sFile);
            File.Copy(sTemplate, sFile);        ApplicationClass oExcel = null;
            Workbooks oBooks = null;
            Workbook oBook = null;
            Worksheet oSheet = null;
            Range oRow = null;
            Range oRange = null;        object paramMissing = Type.Missing;
            //XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
            //XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
            //bool paramOpenAfterPublish = false;
            //bool paramIncludeDocProps = true;
            //bool paramIgnorePrintAreas = true;
            object paramFromPage = Type.Missing;
            object paramToPage = Type.Missing;
            //bool bFlag = false;
            System.Diagnostics.Process p = null;
            try
            {
                oExcel = new ApplicationClass();
                oExcel.Visible = false;
                oExcel.DisplayAlerts = false;
                oBooks = oExcel.Workbooks;
                oBook = oBooks.Open(sFile, paramMissing, false, paramMissing,
                                                paramMissing, paramMissing, paramMissing, paramMissing, paramMissing,
                                                true, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing);
                IntPtr t = new IntPtr(oExcel.Hwnd);
                int k = 0;
                GetWindowThreadProcessId(t, out k);
                p = System.Diagnostics.Process.GetProcessById(k);
                
            if (oBook != null)
                {
                    oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);                int row = 4;
                    Object typeMissing = System.Type.Missing;
                    string sCell = "A" + row + 10;
                    oRow = oSheet.get_Range(sCell, typeMissing);
                    oRange = oRow.EntireRow;
                    oRange.Select();
                    oRange.Copy(typeMissing);
                    oRange.Insert(XlInsertShiftDirection.xlShiftDown, Microsoft.Office.Interop.Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow);
                    oRange.PasteSpecial(XlPasteType.xlPasteAll, XlPasteSpecialOperation.xlPasteSpecialOperationAdd, typeMissing, typeMissing);
                    oSheet.Cells[2, 9] = DateTime.Now.ToString("yyyy/MM/dd");
                    if (ds.Rows.Count > 0)
                    {
                        for (int j = 0; j < ds.Rows.Count; j++)
                        {
                            oSheet.Cells[row + j, 1] = ds.Rows[j]["cdept"].ToString().Trim();
                            oSheet.Cells[row + j, 2] = ds.Rows[j]["m_desc"].ToString().Trim();
                            oSheet.Cells[row + j, 3] = DateTime.Parse(ds.Rows[j]["indate"].ToString()).ToString("yyyy/MM/dd");
                            oSheet.Cells[row + j, 4] = ds.Rows[j]["a1"].ToString().Trim();
                            oSheet.Cells[row + j, 5] = ds.Rows[j]["a2"].ToString().Trim();
                            oSheet.Cells[row + j, 6] = ds.Rows[j]["a3"].ToString().Trim();
                            oSheet.Cells[row + j, 7] = ds.Rows[j]["a4"].ToString().Trim();
                            oSheet.Cells[row + j, 8] = ds.Rows[j]["a5"].ToString().Trim();
                            oSheet.Cells[row + j, 9] = ds.Rows[j]["a6"].ToString().Trim();
                        }
                    }
                    //if (!bPrint && !bExcel)
                    //    oBook.ExportAsFixedFormat(paramExportFormat,
                    //         sFile, paramExportQuality,
                    //         paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
                    //         paramToPage, paramOpenAfterPublish,
                    //         paramMissing);
                    //bFlag = true;
                }
            }
            catch (Exception ex)
            {
                comm.jsAlert(ex.Message, page);
                //bFlag = false;
            }
            finally
            {
                if (bPrint)
                    oBook.PrintOut(paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing);            if (oBook != null)
                {
                    oBook.Close(true, paramMissing, paramMissing);
                }            if (oBooks != null)
                {
                    oBooks.Close();
                    oBooks.Application.Quit();
                }            if (oExcel != null)
                {
                    oExcel.Quit();
                }            oSheet = null;
                oBook = null;
                oBooks = null;
                oExcel = null;            if (p != null) p.Kill();            //if (!bPrint && !bExcel)
                //{
                //    File.Delete(sFile);
                //    OpenFile(sFile + ".pdf", "application/pdf", ".pdf", page, true);
                //}
                //else
                OpenFile(sFile, "application/excel", ".xls", page, true);
            }
            return true;
        }
        public void OpenFile(string sFile, string sType, string sSubFileName, System.Web.UI.Page page, bool bDeleteFile)
        {
            FileStream fs = File.OpenRead(sFile);
            BinaryReader br = null;
            if (fs != null)
            {
                br = new BinaryReader(fs);
            }
            BinaryWriter bw = new BinaryWriter(page.Response.OutputStream);
            byte[] buffer = new byte[1024];
            int iCount = br.Read(buffer, 0, buffer.Length);
            while (iCount > 0)
            {
                bw.Write(buffer, 0, iCount);
                iCount = br.Read(buffer, 0, buffer.Length);
            }
            br.Close();
            bw.Close();        if (bDeleteFile) File.Delete(sFile);        page.Response.ContentType = sType;
            page.Response.AddHeader("Content-disposition", "attachment; filename=Report" + sSubFileName);
            page.Response.End();    }
    這個是 將資料轉換成EXCEL檔的一些代碼,你自己參考一下。
      

  3.   

    那个思路已经 被否了,打印office系列的产品有没有不调用com 的方法啊。高手继续
      

  4.   

    1.不明白IfilePrint是什么东西..2.另外 "IfilePrint(byte[] br);实现这个接口,但有一条不许把这个流再写到本地(转换成本地文件)"
    既然IfilePrint是接口了,你还管得到别人实现这个接口的时候内部做什么事?如果想要限制,就弄一个seal的方法,或者直接静态方法..不过话说回来了,byte[] br是调用方提供的,你如何限制调用者不把 byte[] br 写成本地文件?
      

  5.   

    用 水晶报表 可以打印,也可以直接导出excel,打印!