Пример #1
0
        public virtual void HandleCommand(PrintingSystemCommand command, object[] args, IPrintControl control, ref bool handled)
        {
            if (!CanHandleCommand(command, control))
            {
                return;
            }

            XtraReport1 report = new XtraReport1();
            Stream      myStream;

            report.Parameters["IsExporting"].Value = true;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter           = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex      = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    report.ExportToXls(myStream);

                    myStream.Close();
                }
            }


            // Set handled to true to avoid the standard exporting procedure to be called.
            handled = true;
        }
        internal static void Export(string fileName, XlsExportOptions xlsExportOptions)
        {
            XtraReport1  report = new XtraReport1();
            nwindDataSet ds     = new nwindDataSet();

            nwindDataSetTableAdapters.CategoriesTableAdapter cta = new WindowsApplication1.nwindDataSetTableAdapters.CategoriesTableAdapter();
            cta.Fill(ds.Categories);

            report.Category.Value = ds.Categories[0].CategoryID;
            report.PrintingSystem.ExportOptions.PrintPreview.ShowOptionsBeforeExport = false;
            report.CreateDocument();
            report.ExportToXls(fileName, xlsExportOptions);

            Excel.Application ObjExcel = new Excel.Application();
            Workbook          ObjWorkBookGeneral;
            Worksheet         ObjWorkSheet;
            Workbook          ObjWorkBookTemp = ObjExcel.Workbooks.Open(Environment.CurrentDirectory + "\\" + fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            ObjWorkBookGeneral = ObjExcel.Workbooks.Open(Environment.CurrentDirectory + "\\" + fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            ((Worksheet)ObjWorkBookGeneral.Sheets[1]).Name = ds.Categories[0].Description.Length > 30 ? ds.Categories[0].Description.Substring(0, 30) : ds.Categories[0].Description;

            bool isObjWorkBookTempClosed = false;

            try {
                for (int i = 1; i < ds.Categories.Count; i++)
                {
                    report.Category.Value = ds.Categories[i].CategoryID;
                    report.CreateDocument();
                    report.ExportToXls("temp_" + fileName + ".xls");
                    ObjWorkBookTemp         = ObjExcel.Workbooks.Open(Environment.CurrentDirectory + "\\temp_" + fileName + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    isObjWorkBookTempClosed = false;
                    ObjWorkSheet            = (Microsoft.Office.Interop.Excel.Worksheet)ObjWorkBookTemp.Sheets[1];
                    ObjWorkSheet.Name       = ds.Categories[i].Description.Length > 30 ? ds.Categories[i].Description.Substring(0, 30) : ds.Categories[i].Description;
                    ObjWorkSheet.Copy(Type.Missing, ObjWorkBookGeneral.Sheets[ObjWorkBookGeneral.Sheets.Count]);
                    ObjWorkBookTemp.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
                    isObjWorkBookTempClosed = true;
                }
            } finally {
                if (!isObjWorkBookTempClosed)
                {
                    ObjWorkBookTemp.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
                }
                ObjWorkBookGeneral.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
                ObjExcel.Quit();
                File.Delete(Environment.CurrentDirectory + "\\temp_" + fileName + ".xls");
            }
        }
 private void btnExportXLS_Click(object sender, EventArgs e)
 {
     // Export the report to XLS.
     report.ExportToXls("report.xls");
 }