Convert excel file to jpg in c# - c#

I am trying to use Spire dll to convert excel file to jpg .
so i have a xlsm file like this :
i use this code to convert it to jpg :
Workbook workbook = new Workbook();
workbook.LoadFromFile(#"D:\a.xlsm");
Worksheet sheet = workbook.Worksheets[1];
sheet.SaveToImage("exceltoimage.jpg");
System.Diagnostics.Process.Start("exceltoimage.jpg");
but the output is like this some of the cell can't be converted why i mean (#name)?
I have this warning in my page :

I finally use Aspose :
http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/category1129.aspx
example :
http://www.aspose.com/docs/display/cellsnet/Converting+Worksheet+to+Image
my sample code :
Workbook workbook = new Workbook(#"D:\a.xlsm");
//Get the first worksheet.
Worksheet sheet = workbook.Worksheets[12];
//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
//Specify the image format
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
//Only one page for the whole sheet would be rendered
imgOptions.OnePagePerSheet = true;
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, imgOptions);
//Render the image for the sheet
Bitmap bitmap = sr.ToImage(0);
//Save the image file specifying its image format.
bitmap.Save(#"d:\SheetImage.jpg");
It works great for me .

Related

Syncfunsio xslio exports null instead of blank in excel in C#

I am using Syncfusion library to generate an excel report. I referred syncfusion to generate the report using template makers. The data is correctly exported in excel file but it is exporting NULL instead of blank string for some of nullable number columns. How can I write blank instead of NULL in excel for nullable fields.
Template file:
Output:
This is the code I am using.
using Syncfusion.XlsIO;
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2010;
IWorkbook workbook = application.Workbooks.Open(_templateFilePath);
IWorksheet worksheet = workbook.Worksheets[0];
ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor();
var data = _database.GetData();
marker.AddVariable("DATA", data);
marker.ApplyMarkers();
workbook.SaveAs(_stReportFilePath);
workbook.Close();
excelEngine.Dispose();
}

Formatting not retain in closedXML

I am creating a XLSX file using XLWorkbook (ClosedXML.Excel).
I want to format the columns to percentage/decimal/number etc.
I have used the following
oSheet.Range("CL2:CL200").Style.NumberFormat.SetFormat("0.00%");
when I checked the excel its still showing the result as 0.2030 i.e without percentage. But when I double click the column format change to percentage (20.30%).
Why column not showing percentage when I first open the excel i.e. without double click.
How can I retain the format after creating excel in c# code
Code sample is :
DataTable oDataTable = GetDataTable(oData);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(oDataTable, "Sheet1");
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
wb.Style.Font.Bold = true;
IXLWorksheet oSheet;
wb.TryGetWorksheet("Sheet1", out oSheet);
ange("B1:B200").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
oSheet.Range("C1:C200").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
oSheet.Range("D1:D200").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
oSheet.Range("CL2:CL200").Style.NumberFormat.SetFormat("0.00%");
oSheet.Column("CL").AdjustToContents();
wb.SaveAs(fullPath);
}
After double clicking I an getting the correct format as :

Export Excel To PDF excludign hidden tabs

I am trying to export Excel files to PDF. I am having success with this using the Microsoft.Office.Interop namespace. I am now trying to find out how to exclude tabs that are marked hidden, so that they are not within the PDF> Hase anyone done this or knows how to do this? My code is shown below that I am currently using.
string inFile = #"C:\Users\casey.pharr\Desktop\testPDF\3364850336.xls";
string outFile = #"C:\Users\casey.pharr\Desktop\testPDF\3364850336_noHidden_out.pdf";
string tempFile = #"C:\Users\casey.pharr\Desktop\testPDF\temp.xls";
try
{
//first copy original file to temp file to work with
File.Copy(inFile,tempFile, true);
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
app.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open(tempFile);
for(int x = app.Sheets.Count-1; x-1 > 1; x--)
{
Excel._Worksheet sheet = (Excel._Worksheet)app.Sheets[x];
//now delete hidden worksheets from work book. This is why we are using tempFile
if (sheet.Visible == Microsoft.Office.Interop.Excel.XlSheetVisibility.xlSheetHidden || sheet.Visible == Microsoft.Office.Interop.Excel.XlSheetVisibility.xlSheetVeryHidden && sheet != null)
{
//is sheet hidden. If so remove it so not part of converted file
sheet.Delete();
}
}
wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outFile);
wkb.Close(false);
app.Quit();
//return outputLocation;
The error that occurs on calling .Delete() is below:
Exception from HRESULT: 0x800A03EC
enter code here
So we can convert the pdf's fine, but not remove or exclude hidden worksheets. I went the route to try to delete them then convert the entire file, but not working.

Trying to create an excel chart in a XLS file with data

My problem currently is that the data shows up like it's suppose to in the excel file but the chart doesn't appear correctly. I am trying Method 1: http://www.dotnetperls.com/excel.
I have already looked at this: Excel Interop Apply Chart Template
Charts: (http://imgur.com/a/btgWT)
The top image is what I get the bottom image is what I want.
string tempPath = Path.GetTempPath();
//C:\appdata...\LicenseCheck
string folderPath = string.Format(#"{0}\LicenseCheck", tempPath);
//The csv file with the XMAX license usage stats
string secondPath = string.Format(#"{0}\{1}_Report_Stats_{2}_{3}.csv",
folderPath, productName, lastCheckedDate.Month, lastCheckedDate.Year);
var application = new Application();
var workbook = application.Workbooks.Open(secondPath);
var worksheet = workbook.Worksheets[1] as Worksheet;
object misValue = System.Reflection.Missing.Value;
// Add chart.
var charts = worksheet.ChartObjects() as ChartObjects;
var chartObject = charts.Add(60, 10, 300, 300) as ChartObject;
var chart = chartObject.Chart;
// Set chart range.
var range = worksheet.get_Range("A1", "C25");
chart.SetSourceData(range);
// Set chart properties.
chart.ChartType = XlChartType.xlColumnClustered;
chart.ChartWizard
(
Source: range
// Title: "graphTitle",
// CategoryTitle: "xAxis",
// ValueTitle: "yAxis"
);
chartObject.Chart.ApplyChartTemplate(#"C:\\LicenseUsageStatsTemplate.crtx");
// Save.
workbook.SaveAs("Stats.xls", XlFileFormat.xlWorkbookNormal);
workbook.Close(true, misValue, misValue);
application.Quit();
releaseObject(worksheet);
releaseObject(workbook);
releaseObject(application);
Found out that MS office had to be installed on the server to use Interop. So, ended up using EPPlus. It's an awesome library that uses OfficeOpenXML.

Epplus read hyperlink with html fragment i

I got Excel xlsx document with hyperlinks.
Hyperlinks have adresses and subaddresses (that's the way VBA call Html fragments, all after # sign)
Epplus library has Hyperlink property for every cell, but it has only first part of html address, so instead of
stackoverflow.com#footer
I got:
stackoverflow.com
Is there any way to read the html fragment part with this library ?
Code for reading hyperlinks via epplus:
FileInfo xlsxFile = new FileInfo(_filePath);
using (ExcelPackage pck = new ExcelPackage(xlsxFile))
{
var wb = pck.Workbook;
if (wb == null)
return null;
var ws = wb.Worksheets.FirstOrDefault();
ExcelRange er = ws.Cells[0,0];
var hyperlink = er.Hyperlink;
It seems to be an issue with the way excel store hyperlinks and the way Epplus reads them. Excel stores the hyperlinks both in the worksheet itself as well as the relationship file for the worksheet which is a file that stores any kind of cross referencing between workbook parts (worksheets, styles, strings, etc). This all has to do with the structure of the an xlsx file which is xml based off of the OpenOffice XML standard: OpenOffice XML Info
So the problem is Epplus is relying on that relationship file which does not contain the fragment while the `hyperlink' node in the worksheet xml does. You can see all of this in its gory detail if you open up the xlsx file as a zip file by renaming it.
So, the short answer is you are forced to use the `.Value' of the cell object. Not as clean but it will work. For example, if I create a cell like this:
with this code:
var fi = new FileInfo(#"c:\temp\Html_Fragment.xlsx");
using (var pck = new ExcelPackage(fi))
{
var wb = pck.Workbook;
var ws = wb.Worksheets.FirstOrDefault();
ExcelRange er = ws.Cells[1,1];
var hyperlink = er.Hyperlink;
Console.WriteLine(er.Value);
Console.WriteLine("{{Value: {0}, Hyperlink: {1}}}", er.Value, er.Hyperlink.AbsoluteUri);
}
Gives this:
{
Value: https://msdn.microsoft.com/en-us/library/aa982683(v=office.12).aspx#Anchor_3,
Hyperlink: https://msdn.microsoft.com/en-us/library/aa982683(v=office.12).aspx
}

Categories

Resources