コード例 #1
0
        private void btn_imprimir_Click(object sender, EventArgs e)
        {
            string root     = @"N:\EXCEL";
            String fileName = @"N:\EXCEL\";

            if (!Directory.Exists(root))
            {
                Directory.CreateDirectory(root);
            }
            try
            {
                btn_imprimir.Enabled = false;
                int index = grd_Facturas.SelectedCells[0].RowIndex;

                objDocumentoCab = objListaDocumentoCab[index];

                formatearLetra();
                fileName = fileName + objDocumentoCab.DocumentoCabSerie.Substring(0, 1) + "-" + objDocumentoCab.DocumentoCabNro + ".xls";
                Letras cr = new Letras();


                cr.SetDataSource(objListaLetraReporte);

                ExportOptions              exportOpts      = new ExportOptions();
                ExcelFormatOptions         excelFormatOpts = new ExcelFormatOptions();
                DiskFileDestinationOptions diskOpts        = new DiskFileDestinationOptions();
                exportOpts = cr.ExportOptions;

                // Set the excel format options.
                excelFormatOpts.ExcelUseConstantColumnWidth = true;
                excelFormatOpts.ExcelTabHasColumnHeadings   = true;
                excelFormatOpts.ShowGridLines = true;
                exportOpts.ExportFormatType   = ExportFormatType.Excel;
                exportOpts.FormatOptions      = excelFormatOpts;

                // Set the disk file options and export.
                exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                diskOpts.DiskFileName            = fileName;
                exportOpts.DestinationOptions    = diskOpts;

                cr.Export();

                PrintMyExcelFile(fileName);
                btn_imprimir.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                btn_imprimir.Enabled = true;
            }
        }
コード例 #2
0
        private void btn_excel_Click(object sender, EventArgs e)
        {
            try
            {
                btn_excel.Enabled = false;
                // String fileName = @"C:\FACTURACION\EXCEL\";
                //String path = @"C:\FACTURACION\EXCEL";
                String fileName = @"N:\EXCEL\";
                String path     = @"N:\EXCEL";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                int index = grd_Facturas.SelectedCells[0].RowIndex;

                objDocumentoCab = objListaDocumentoCab[index];
                formatearLetra();
                Random rnd       = new Random();
                int    randomnum = rnd.Next(1000);


                fileName = fileName + objDocumentoCab.DocumentoCabSerie.Substring(0, 1) + " - " + objDocumentoCab.DocumentoCabNro + "-" + randomnum + ".xls";
                Letras cr = new Letras();


                cr.SetDataSource(objListaLetraReporte);

                ExportOptions              exportOpts      = new ExportOptions();
                ExcelFormatOptions         excelFormatOpts = new ExcelFormatOptions();
                DiskFileDestinationOptions diskOpts        = new DiskFileDestinationOptions();
                exportOpts = cr.ExportOptions;

                // Set the excel format options.
                excelFormatOpts.ExcelUseConstantColumnWidth = true;
                excelFormatOpts.ExcelTabHasColumnHeadings   = true;
                excelFormatOpts.ShowGridLines = true;
                exportOpts.ExportFormatType   = ExportFormatType.Excel;
                exportOpts.FormatOptions      = excelFormatOpts;

                // Set the disk file options and export.
                exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                diskOpts.DiskFileName            = fileName;
                exportOpts.DestinationOptions    = diskOpts;

                cr.Export();
                FileInfo fi = new FileInfo(fileName);
                if (fi.Exists)
                {
                    System.Diagnostics.Process.Start(fileName);
                }
                else
                {
                    //file doesn't exist
                }

                btn_excel.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR :" + ex.Message);
                btn_excel.Enabled = true;
            }
        }