I open an Excel file in c#, make some changes and I want to save it as pdf file.
I have searched about it and find this:
Microsoft.Office.Interop.Excel._Workbook oWB;
oWB.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, "D:\\xxxxx.pdf");
but this code sometimes opens a form and a printer must be selected! I don't know why?!
Is there any other way for exporting PDF from Excel?
I saw that Workbook.saveas() has a Fileformat object. How can we use it?
Check out Spire.Xls, below is the code for converting Excel to PDF.
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
//If you want to make the excel content fit to pdf page
//workbook.ConverterSetting.SheetFitToPage = true;
workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF);
It has a free version which you can use without charge:
https://www.e-iceblue.com/Introduce/free-xls-component.html (Also available on NuGet)
Use iTextSharp. It is native .NET code. Doesn't require any Excel interop -
https://www.nuget.org/packages/itextsharp/
If you're looking for an alternative approach, then check out GemBox.Spreadsheet:
https://www.nuget.org/packages/GemBox.Spreadsheet/
Here is how you can save Excel to PDF with it:
ExcelFile excel = ExcelFile.Load("D:\\xxxxx.xlsx");
excel.Save("D:\\xxxxx.pdf");
Or you can write it as following:
XlsxLoadOptions loadOptions = new XlsxLoadOptions();
ExcelFile excel = ExcelFile.Load("D:\\xxxxx.xlsx", loadOptions);
PdfSaveOptions saveOptions = new PdfSaveOptions();
excel.Save("D:\\xxxxx.pdf", saveOptions);
You can do this using this API. Please see documentation for further detail.
http://cdn.bytescout.com/help/BytescoutPDFExtractorSDK/html/55590148-5bef-4338-ac16-1de4056a952b.htm
Related
I have to use the data from third-party DB.
and they provide us excel format with addins, when i open the excel, then i can see the data after few seconds( just like bloomberg).
NORMAL_OPEN_Without_C#
But_like_this_TT
and now i want to use this data automatically but they don't have api provide.
so i try to open the excel with C# programming ,and save&quit the file after few minutes later.
but i cannot see the ribbon menu when i open the file with C# program, the result of the formula also does not work .
a.cs
Application app = new Application();
app.Visible = true;
Workbook wb = app.Workbooks.Open(Filename: #"C:\nn\a.xlsx");
Console.ReadLine();
wb.Save();
wb.Close();
app.Quit();
i want to know the way to have the result of formula from external-addins.
if there is another way to have the result plz. let me know TT
thanks.
I have tried this;
var excelPicture = sheet.Drawings[0] as OfficeOpenXml.Drawing.ExcelPicture;
var img = excelPicture.Image;
However the excelPicture variable become null. How can I create image file from ExcelDrawing??
It seems that this can't be done via EPPlus API. There is the opened issue in the GitHub project repository how export the drawings to a file. But the provided solution in the question does not work (it seems #Onchomngebul himself wrote the the comment)
Alternative solution is to use Workbook class in Spire.XLS nuget-package (or via free version FreeSpire.XLS).
var workbook = new Workbook();
workbook.LoadFromFile(workbookFileName, ExcelVersion.Version2010);
var sheet = workbook.Worksheets[0]; // index or name of your worksheet
var image = workbook.SaveChartAsImage(sheet, 0); // chart index
img.Save(chartFileName, ImageFormat.Png);
For more details please see this comment in the issue.
Another solution is to try to use Excel.ChartObject. I think this question Exporting Excel Charts as Images may help you.
Is it possible to set the excel filename before file saving?
I have following simple code:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excel = new Excel.Application();
excel.Visible = true;
Excel.Workbook workbook = excel.Workbooks.Add(Excel.XlSheetType.xlWorksheet);
Excel.Worksheet sheet = workbook.Sheets[1];
sheet.Cells[1, 1] = "Hello World!";
Is it possible to predefine this name before saving?
Thanks.
There is no explicit, foolproof way to do this prior to saving, unfortunately. The closest you could come is to use a template. If you have a template called FOO.xltx, you could create your workbook like this:
Application.Workbooks.Add "X:\path\to\FOO.xltx"
The only quirk is that the name for the new documents will be appended with an incrementing number (FOO1 the first time, then FOO2,FOO3, etc.).
To create a template, just create a new document, and when you save it, select Excel Template (*.xltx) from the Save as type dropdown.
You have to use saveas to save the file with the filename you want. Then when the user clicks save it will just update the file that was previously created. Unfortunately there is no other way. Here is the code:
workbook.SaveAs(Filename: FILENAMEHERE);
Here is the MSDN doc for it: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas.aspx
I need to read XLTM files without opening it.
With Excel interop, i can read but it will open the file too.
The below link shows reading xlsx file with OLDB. But same wont work for XLTM.
http://codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/
Is there any way i can read XLTM file with out opening the file it self.
Thanks in Advance.
You can definitely use the Excel interop assembly, just set visibility and screenUpdation off like :
Microsoft.Office.Interop.Excel.Application xltmApp = new Microsoft.Office.Interop.Excel.Application();
xltmApp.Visible = false;
xltmApp.ScreenUpdating = false;
Workbook xltmBook = xltmApp.Workbooks.Open(#"C:\test.xltm");
...do stuff
Then close document properly see: http://msdn.microsoft.com/en-us/library/h1e33e36.aspx
Also if you want you can turn off dialog boxes during saving see: Trying to exit C# Excel Workbook without a dialog box.
So the company I'm working for is looking for a means to verify that a given .xls/.xlsx file is valid. Which means checking columns and rows and other data. He's having me evaluate GrapeCity Spread and SpreadsheetGear, but I'm wondering if anyone else has any other suggestions of external tools to check out.
We don't need a means to export .xls files or anything like that, just the ability to import them and verify they are valid based on a set of criteria I create.
Thanks.
If you need just to compare cell values you can use ADO.NET driver, for anything else will be required Excel or third party component. I am using SpreadsheetGear. When I was evaluating this component 3 years ago I have found an issue with conditional formatting for cell with absolute reference, but issue was quickly resolved. They have same day support response.
To my mind, the easiest way to handle this is to use an ODBC Excel data provider. I find it more straightforward to work with than the PIAs.
// Connection string for Excel 2007 (.xlsx)
string dbConnStr = #"Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dsn=Excel Files;dbq=C:\temp\mySpreadsheet.xlsx";
// Connection string for Excel 98-2003 (.xls)
//string dbConnStr = #"Driver={Microsoft Excel Driver (*.xls)};driverid=790;dbq=C:\temp\mySpreadsheet.xls;defaultdir=c:\temp";
OdbcCommand cmd = new OdbcCommand("Select * from [SheetName$]", new OdbcConnection(dbConnStr));
cmd.Connection.Open();
OdbcDataReader dr = cmd.ExecuteReader();
foreach (System.Data.IDataRecord item in dr)
{
// Check specific column values, etc
string id = item["Column Name"].ToString();
}
You can use the Microsoft.Office.Interop.Excel library to access any workbook the same way you do in Excel VBA.
Code looks like this:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Open("datasheet.xls");
Excel.Worksheet worksheet = workbook["Sheet1"] as Excel.Worksheet;
string someData = (worksheet.Range["A2"] as Excel.Range).Value.ToString();
worksheet = null;
workbook.Close();
excel.Quit();
Depending on your budget, the Aspose libraries are great. Not cheap but work very, very well.
you can use the oleDb from Microsoft to access the excel data as any other database system. You can get the right connection string from connectionstrings
Maybe the NPOI project can be useful (I have never used it though).
Best
Check out Excel Data Reader GitHub (formerly on CodePlex). I've used this a few times and it works well.
Be warned however that there are bugs reading .xlsx files where cells are skipped. Apply this patch (link is to Codeplex and out of date) I submitted for v2.0.1.0 to fix the problem. (The project maintainers don't seem active and I've had problems contacting them.)