Пример #1
0
        private bool ConvertExcelToPdf(string tempPath, string outputPath)
        {
            // If either required string is null or empty, stop and bail out
            if (string.IsNullOrEmpty(tempPath) || string.IsNullOrEmpty(outputPath))
            {
                return(false);
            }

            // Create COM Objects

            // Create new instance of Excel
            var excelApplication = new Microsoft.Office.Interop.Excel.Application
            {
                ScreenUpdating = false,
                DisplayAlerts  = false
            };

            // Make the process invisible to the user

            // Make the process silent

            // Open the workbook that you wish to export to PDF
            var excelWorkbook = excelApplication.Workbooks.Open(tempPath);

            // If the workbook failed to open, stop, clean up, and bail out
            if (excelWorkbook == null)
            {
                excelApplication.Quit();
                return(false);
            }

            var exportSuccessful = true;

            try
            {
                // Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
                excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                // Mark the export as failed for the return value...
                exportSuccessful = false;
            }
            finally
            {
                // Close the workbook, quit the Excel, and clean up regardless of the results...
                excelWorkbook.Close();
                excelApplication.Quit();
            }


            return(exportSuccessful);
        }
        public ResponseModel ConvertExcelToPdf(string fileLocation, string outLocation)
        {
            Microsoft.Office.Interop.Excel.Application app = null;
            Workbooks workbooks = null;
            Workbook  wkb       = null;

            try
            {
                if (!File.Exists(outLocation))
                {
                    app               = new Microsoft.Office.Interop.Excel.Application();
                    app.Visible       = false;
                    app.DisplayAlerts = false;
                    app.Interactive   = false;
                    workbooks         = app.Workbooks;
                    wkb               = workbooks.Open(fileLocation, ReadOnly: true);
                    wkb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outLocation);

                    Marshal.ReleaseComObject(workbooks);
                    wkb.Close();
                    Marshal.ReleaseComObject(wkb);
                    app.Quit();
                    Marshal.ReleaseComObject(app);
                }
                else
                {
                    return(new ResponseModel {
                        IsSucceed = false, ErrorMessage = outLocation + " file-ı artıq mövcuddur."
                    });
                }

                if (!File.Exists(outLocation))
                {
                    return(new ResponseModel {
                        IsSucceed = false, ErrorMessage = outLocation + " file-ı tapılmadı."
                    });
                }
                return(new ResponseModel {
                    Data = outLocation, IsSucceed = true, ErrorMessage = string.Empty
                });
            }
            catch (Exception e)
            {
                Marshal.ReleaseComObject(workbooks);
                wkb.Close();
                Marshal.ReleaseComObject(wkb);
                app.Quit();
                Marshal.ReleaseComObject(app);
                return(new ResponseModel {
                    IsSucceed = false, ErrorMessage = "File convert oluna bilmədi: " + e.Message
                });
            }
        }
Пример #3
0
        private static Uri ProcessExcelDocument(string fullFileName)
        {
            string tempFile = string.Format("{0}\\{1}.html",
                                            Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),
                                            Guid.NewGuid());
            object      oMissing    = Missing.Value;
            object      oReadOnly   = true;
            object      oFileType   = XlFileFormat.xlHtml;
            CultureInfo saveCulture = Thread.CurrentThread.CurrentCulture;

            Microsoft.Office.Interop.Excel.Application application = null;
            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                application = new Microsoft.Office.Interop.Excel.Application();
                Workbook document = application.Workbooks.Open(fullFileName, oMissing, oReadOnly, oMissing,
                                                               oMissing, oMissing, oMissing, oMissing, oMissing,
                                                               oMissing, oMissing, oMissing, oMissing, oMissing,
                                                               oMissing);
                if (document != null)
                {
                    document.SaveAs(tempFile,                       //File Name
                                    oFileType,                      //File Format
                                    oMissing,                       //Password
                                    oMissing,                       //Write Res Password
                                    oMissing,                       //ReadOnly Recommended
                                    oMissing,                       //Create Backup
                                    XlSaveAsAccessMode.xlExclusive, //AccessMode
                                    oMissing,                       //Conflict Resolution
                                    oMissing,                       //Add To MRU
                                    oMissing,                       //Text Codepage
                                    oMissing,                       //Text Visual Layout
                                    oMissing                        //Local
                                    );
                }
            }
            finally
            {
                if (application != null)
                {
                    application.Quit();
                }
                Thread.CurrentThread.CurrentCulture = saveCulture;
            }
            return(new Uri(tempFile));
        }
Пример #4
0
 /// <summary>
 ///     转换Excel文档成Html
 /// </summary>
 /// <param name="sourcePath"></param>
 /// <param name="targetPath"></param>
 /// <returns></returns>
 public static ConvertResult ExcelToHtml(string sourcePath, string targetPath)
 {
     try
     {
         var      excelApp = new Microsoft.Office.Interop.Excel.Application();
         Workbook workbook = excelApp.Application.Workbooks.Open(sourcePath);
         workbook.SaveAs(targetPath, XlFileFormat.xlHtml);
         workbook.Close();
         excelApp.Quit();
         ChanageCharset(targetPath);
         return(new ConvertResult {
             IsSuccess = true, Message = targetPath
         });
     }
     catch (Exception ex)
     {
         Log("转换Excel文档成Html文档时异常", ex.Message + ex.StackTrace);
         return(new ConvertResult {
             IsSuccess = false, Message = ex.Message + ex.StackTrace
         });
     }
 }
Пример #5
0
        private void saveAppartmentsCalc_Click(object sender, EventArgs e)
        {
            var excelApp = new Microsoft.Office.Interop.Excel.Application();

            //Microsoft.Office.Interop.Excel.Workbook excelDoc = new Microsoft.Office.Interop.Excel.Workbook();
            string v = excelApp.Version;

            //excelApp.;
            string ownerFullName = ownerSurname.Text + " " + ownerName.Text + " " + ownerInit.Text;
            string customerFullName = customerSurname.Text + " " + customerName.Text + " " + customerInit.Text;
            string fileName = "отчет " + contractNum.Text + " расчет стоимости квартиры " + appartmentNum.Text + " " +
                              street.Text + " " + houseNum.Text + " для " + bankName.Text;
            saveFileDialog1.FileName = fileName.Replace("\"", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace("/", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace(",", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace("№", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace(".", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace("-", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace("  ", " ").ToLower();

            if (DialogResult.OK == saveFileDialog1.ShowDialog())
            {
                excelApp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\calc.xls", Missing, Missing,
                                        Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing,
                                        Missing, Missing, Missing);

                //первый аналог
                excelApp.Workbooks[1].Sheets[1].Cells[2, 3] = calculationAppartaments[2, 0].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[2, 4] = calculationAppartaments[3, 0].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[2, 5] = calculationAppartaments[4, 0].Value;

                //                excelApp.Workbooks[1].Sheets[1].Cells[2, 6] = Convert.ToDateTime(analogsGrid.Rows[18].Cells[3].Value.ToString()).ToLongDateString();
                excelApp.Workbooks[1].Sheets[1].Cells[3, 3] = calculationAppartaments[2, 1].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[3, 4] = calculationAppartaments[3, 1].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[3, 5] = calculationAppartaments[4, 1].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[5, 3] = calculationAppartaments[2, 3].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[5, 4] = calculationAppartaments[3, 3].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[5, 5] = calculationAppartaments[4, 3].Value;
                string pattern = "MMMM yyyyг.";
                string d1 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[2].Value.ToString()).ToString(pattern);
                string d2 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[3].Value.ToString()).ToString(pattern);
                string d3 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[4].Value.ToString()).ToString(pattern);
                excelApp.Workbooks[1].Sheets[1].Cells[7, 3] = d1;
                excelApp.Workbooks[1].Sheets[1].Cells[7, 4] = d2;
                excelApp.Workbooks[1].Sheets[1].Cells[7, 5] = d3;

                excelApp.Workbooks[1].Sheets[1].Cells[8, 3] = calculationAppartaments[2, 6].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[8, 4] = calculationAppartaments[3, 6].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[8, 5] = calculationAppartaments[4, 6].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[10, 3] = calculationAppartaments[2, 8].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[10, 4] = calculationAppartaments[3, 8].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[10, 5] = calculationAppartaments[4, 8].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[12, 3] = calculationAppartaments[2, 10].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[12, 4] = calculationAppartaments[3, 10].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[12, 5] = calculationAppartaments[4, 10].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[14, 3] = calculationAppartaments[2, 12].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[14, 4] = calculationAppartaments[3, 12].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[14, 5] = calculationAppartaments[4, 12].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[16, 3] = calculationAppartaments[2, 14].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[16, 4] = calculationAppartaments[3, 14].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[16, 5] = calculationAppartaments[4, 14].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[18, 3] = calculationAppartaments[2, 16].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[18, 4] = calculationAppartaments[3, 16].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[18, 5] = calculationAppartaments[4, 16].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[20, 3] = calculationAppartaments[2, 18].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[20, 4] = calculationAppartaments[3, 18].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[20, 5] = calculationAppartaments[4, 18].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[22, 3] = calculationAppartaments[2, 20].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[22, 4] = calculationAppartaments[3, 20].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[22, 5] = calculationAppartaments[4, 20].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[24, 3] = calculationAppartaments[2, 22].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[24, 4] = calculationAppartaments[3, 22].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[24, 5] = calculationAppartaments[4, 22].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[26, 3] = calculationAppartaments[2, 24].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[26, 4] = calculationAppartaments[3, 24].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[26, 5] = calculationAppartaments[4, 24].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[28, 3] = calculationAppartaments[2, 26].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[28, 4] = calculationAppartaments[3, 26].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[28, 5] = calculationAppartaments[4, 26].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[30, 3] = calculationAppartaments[2, 28].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[30, 4] = calculationAppartaments[3, 28].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[30, 5] = calculationAppartaments[4, 28].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[32, 3] = calculationAppartaments[2, 30].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[32, 4] = calculationAppartaments[3, 30].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[32, 5] = calculationAppartaments[4, 30].Value;

                excelApp.Workbooks[1].Sheets[1].Cells[34, 3] = calculationAppartaments[2, 32].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[34, 4] = calculationAppartaments[3, 32].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[34, 5] = calculationAppartaments[4, 32].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[35, 3] = calculationAppartaments[2, 33].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[35, 4] = calculationAppartaments[3, 33].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[35, 5] = calculationAppartaments[4, 33].Value;
                excelApp.Workbooks[1].Sheets[1].Cells[38, 3] = calculationAppartaments[2, 36].Value;

                excelApp.ActiveWorkbook.SaveAs(saveFileDialog1.FileName, Type.Missing, Type.Missing, Type.Missing,
                                               Type.Missing, Type.Missing,
                                               XlSaveAsAccessMode.xlNoChange,
                                               Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                excelApp.Quit();
            }
        }
Пример #6
0
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (docTypeT == "Квартира")
            {
                var excelApp = new Microsoft.Office.Interop.Excel.Application();

                excelApp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\Черновик.xls", Missing,
                                        Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing,
                                        Missing, Missing, Missing, Missing);

                int i = 0;
                int j = 0;

                for (i = 0; i <= calculationAppartaments.RowCount - 1; i++)
                {
                    for (j = 2; j <= calculationAppartaments.ColumnCount - 1; j++)
                    {
                        DataGridViewCell cell = calculationAppartaments[j, i];
                        excelApp.Workbooks[1].Sheets[1].Cells[i + 2, j + 1] = cell.Value;
                    }
                }

                excelApp.ActiveWorkbook.Save();
                excelApp.ActiveWorkbook.SaveAs(System.Windows.Forms.Application.StartupPath + "\\" + contractNum.Text + "Calc.xls");
                excelApp.ActiveWorkbook.Close(Missing);
                excelApp.Quit();

                excelApp = new Microsoft.Office.Interop.Excel.Application();

                excelApp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\analogs.xls", Missing, Missing,
                                        Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing,
                                        Missing, Missing, Missing);

                i = 0;
                j = 0;

                for (i = 0; i <= analogsGrid.RowCount - 1; i++)
                {
                    for (j = 1; j <= analogsGrid.ColumnCount - 1; j++)
                    {
                        DataGridViewCell cell = analogsGrid[j, i];
                        excelApp.Workbooks[1].Sheets[1].Cells[i + 2, j + 1] = cell.Value;
                    }
                }

                excelApp.ActiveWorkbook.Save();
                excelApp.ActiveWorkbook.SaveAs(System.Windows.Forms.Application.StartupPath + "\\" + contractNum.Text + "Analogs.xls");
                excelApp.ActiveWorkbook.Close(Missing);
                excelApp.Quit();

                saveState();
            }
            System.Windows.Forms.Application.Exit();
        }
Пример #7
0
        private void button8_Click(object sender, EventArgs e)
        {
            var excelApp = new Microsoft.Office.Interop.Excel.Application();

            //Microsoft.Office.Interop.Excel.Workbook excelDoc = new Microsoft.Office.Interop.Excel.Workbook();
            string ownerFullName = ownerSurname.Text + " " + ownerName.Text + " " + ownerInit.Text;
            string customerFullName = customerSurname.Text + " " + customerName.Text + " " + customerInit.Text;
            string fileName = "отчет " + contractNum.Text + " расчет стоимости земельного участка " + street.Text + " " +
                              houseNum.Text + " для " + bankName.Text;
            saveFileDialog1.FileName = fileName.Replace("\"", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace("/", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace(",", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace("№", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace(".", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace("-", " ").ToLower();
            saveFileDialog1.FileName = saveFileDialog1.FileName.Replace("  ", " ").ToLower();

            if (DialogResult.OK == saveFileDialog1.ShowDialog())
            {
                excelApp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\земля.xls", Missing, Missing,
                                        Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing,
                                        Missing, Missing, Missing);
                int analogsCount = dirtCalcGrid.ColumnCount;
                int rowCount = dirtCalcGrid.RowCount;
                for (int j = 1; j < analogsCount; j++)
                {
                    for (int i = 1; i < rowCount; i++)
                    {
                        if (dirtCalcGrid.Rows[i - 1].Cells[j].Value != null)
                        {
                            excelApp.Workbooks[1].Sheets[1].Cells[i + 1, j + 1] =
                                dirtCalcGrid.Rows[i - 1].Cells[j].Value.ToString();
                        }
                    }
                }

                //первый аналог

                //string pattern = "MMMM yyyyг.";
                //string d1 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[2].Value.ToString()).ToString(pattern);
                //string d2 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[3].Value.ToString()).ToString(pattern);
                //string d2 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[3].Value.ToString()).ToString(pattern);
                //string d2 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[3].Value.ToString()).ToString(pattern);
                //string d2 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[3].Value.ToString()).ToString(pattern);
                //string d2 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[3].Value.ToString()).ToString(pattern);
                //string d3 = Convert.ToDateTime(analogsGrid.Rows[18].Cells[4].Value.ToString()).ToString(pattern);
                //excelApp.Workbooks[1].Sheets[1].Cells[7, 3] = d1;
                //excelApp.Workbooks[1].Sheets[1].Cells[7, 4] = d2;
                //excelApp.Workbooks[1].Sheets[1].Cells[7, 5] = d3;

                excelApp.ActiveWorkbook.SaveAs(saveFileDialog1.FileName, Type.Missing, Type.Missing, Type.Missing,
                                               Type.Missing, Type.Missing,
                                               XlSaveAsAccessMode.xlNoChange,
                                               Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                excelApp.ActiveWorkbook.Close();
                excelApp.Quit();
            }
        }
Пример #8
0
 private static Uri ProcessExcelDocument( string fullFileName )
 {
     string tempFile = string.Format( "{0}\\{1}.html",
                                      Environment.GetFolderPath( Environment.SpecialFolder.InternetCache ),
                                      Guid.NewGuid() );
     object oMissing = Missing.Value;
     object oReadOnly = true;
     object oFileType = XlFileFormat.xlHtml;
     CultureInfo saveCulture = Thread.CurrentThread.CurrentCulture;
     Microsoft.Office.Interop.Excel.Application application = null;
     try
     {
         Thread.CurrentThread.CurrentCulture = new CultureInfo( "en-US" );
         application = new Microsoft.Office.Interop.Excel.Application();
         Workbook document = application.Workbooks.Open( fullFileName, oMissing, oReadOnly, oMissing,
                                                         oMissing, oMissing, oMissing, oMissing, oMissing,
                                                         oMissing, oMissing, oMissing, oMissing, oMissing,
                                                         oMissing );
         if (document != null)
         {
             document.SaveAs( tempFile, //File Name
                              oFileType, //File Format
                              oMissing, //Password
                              oMissing, //Write Res Password
                              oMissing, //ReadOnly Recommended
                              oMissing, //Create Backup
                              XlSaveAsAccessMode.xlExclusive, //AccessMode
                              oMissing, //Conflict Resolution
                              oMissing, //Add To MRU
                              oMissing, //Text Codepage
                              oMissing, //Text Visual Layout
                              oMissing //Local
                 );
         }
     }
     finally
     {
         if (application != null)
             application.Quit();
         Thread.CurrentThread.CurrentCulture = saveCulture;
     }
     return new Uri( tempFile );
 }