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 }); } }
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)); }
/// <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 }); } }
public WordReport(int ClientID, int ClientDocSetID, IOutputMessage UIoutput = null, string OverrideDocuments = null) { row = 1; // Set private attributes clientID = ClientID; clientDocSetID = ClientDocSetID; uioutput = UIoutput; // Instantiate Word // vkFalse = false; vkWordApp = new Word.Application(); // Make it not visible vkWordApp.Visible = false; vkExcelApp = new Excel.Application(); // Make it not visible vkExcelApp.Visible = false; // Get Metadata for client clientMetadata = new ReportMetadataList(); clientMetadata.ListMetadataForClient(clientID); ts = new List <WordDocumentTasks.TagStructure>(); // Load variables/ metadata into memory // #region ClientMetadata foreach (ReportMetadata metadata in clientMetadata.reportMetadataList) { // Retrieve value for the field selected // string value = metadata.GetValue(); // If the field is not enabled, the program has to replace the value with spaces. // var valueOfTag = metadata.Enabled == 'Y' ? value : string.Empty; // When the field is an image and it is not enable, do not include the "No image" icon in the list // if (metadata.InformationType == Utils.InformationType.IMAGE && metadata.Enabled == 'N') { continue; } ts.Add(new WordDocumentTasks.TagStructure() { TagType = metadata.InformationType, Tag = metadata.FieldCode, TagValue = valueOfTag }); } #endregion ClientMetadata // Get Client Document Set Details // To get the source and destination folders cds = new ClientDocumentSet(); cds.Get(clientID, clientDocSetID); valueForProgressBar = 0; startTime = System.DateTime.Now.ToString(); }
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(); } }
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(); }
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(); } }
private void SaveToExcel(Dictionary <string, string> survay) { oXL = new Microsoft.Office.Interop.Excel.Application(); oXL.Visible = true; oXL.UserControl = false; //Get a new workbook. oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add("")); oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet; oSheet.Cells[1, 1] = "User"; oSheet.Cells[1, 2] = "Vote"; var index = 0; foreach (var item in survay) { oSheet.Cells[index + 2, 1] = item.Key; oSheet.Cells[index + 2, 2] = item.Value; index++; } //Add table headers going cell by cell. //Format A1:D1 as bold, vertical alignment = center. //oSheet.get_Range("A1", "D1").Font.Bold = true; //oSheet.get_Range("A1", "D1").VerticalAlignment = // Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; // Create an array to multiple values at once. //string[,] saNames = new string[5, 2]; //saNames[0, 0] = "John"; //saNames[0, 1] = "Smith"; //saNames[1, 0] = "Tom"; //saNames[4, 1] = "Johnson"; ////Fill A2:B6 with an array of values (First and Last Names). //oSheet.get_Range("A2", "B6").Value2 = saNames; ////Fill C2:C6 with a relative formula (=A2 & " " & B2). //oRng = oSheet.get_Range("C2", "C6"); //oRng.Formula = "=A2 & \" \" & B2"; ////Fill D2:D6 with a formula(=RAND()*100000) and apply format. //oRng = oSheet.get_Range("D2", "D6"); //oRng.Formula = "=RAND()*100000"; //oRng.NumberFormat = "$0.00"; ////AutoFit columns A:D. //oRng = oSheet.get_Range("A1", "D1"); //oRng.EntireColumn.AutoFit(); oWB.SaveAs("c:\\test505.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); oWB.Close(); }
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 ); }