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();
}
Related
I am trying to generate an Excel spreadsheet from SQL Server using a datatable. I am not getting any error messages but when I try to save the workbook it is not showing up on my computer. I am using the following code:
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Initialize application
IApplication application = excelEngine.Excel;
//Set the default application version as Excel 2016
application.DefaultVersion = ExcelVersion.Excel2016;
//Create a new workbook
IWorkbook workbook = application.Workbooks.Create(1);
//Access first worksheet from the workbook instance
IWorksheet worksheet = workbook.Worksheets[0];
//Export DataTable to worksheet
//Get data from DataTable
//DataTable dataTable = GetDataTable();
////Export DataTable to worksheet with column name and start range
connection.Open();
SqlCommand cmd = new SqlCommand(PHLIP251, connection);
PHLIP251Datatable.Load(cmd.ExecuteReader());
worksheet.ImportDataTable(PHLIP251Datatable, true, 1, 1);
worksheet.UsedRange.AutofitColumns();
//Save the workbook to disk in xlsx format
workbook.SaveAs(#"Documents\\" + "DMBErrors.xlsx");
connection.Close();
The generated Excel file will be located in the Documents folder created in the bin->Debug folder of the sample. Please use the below code snippet in run-time to get the location of the file.
System.IO.Path.GetFullPath(#"Documents\\" + "DMBErrors.xlsx");
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
}
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 .
Using C# with openxml,I have generated excel document with some data,I added autofilter option in that excel using below code,
string reference= "B6:BA6";
AutoFilter autoFilter1 = new AutoFilter() { Reference = reference };
Worksheet sheet1 = new Worksheet();
sheet1.Append(sheetData);
sheet1.Append(autoFilter1);
Filter working fine,I tried to sort the data using that autofilter option in newly created Excel,but it crashes the entire excel,It displays error message like 'Excel stopped working'...
It seems Excel declares a "definedName" in workbook.xml for the sorting to work.
The one Excel generates looks like this:
...
</sheets>
<definedNames>
<definedName name="_xlnm._FilterDatabase" localSheetId="0" hidden="1">Sheet1!$D$7:$G$7</definedName>
</definedNames>
...
Using the Open XML Sdk to mimic this behaviour seems to do the trick:
...
worksheet.Append(new AutoFilter {Reference = "B6:BA6"});
var definedNames = new DefinedNames();
var definedName = new DefinedName
{
Text = "YourSheetName!$B$6:$BA$6",
Name = "_xlnm._FilterDatabase",
LocalSheetId = 0,
Hidden = true,
};
definedNames.Append(definedName);
spreadsheetDocument.WorkbookPart.Workbook.Append(definedNames);
...
I have several DataTable objects and I want to write them to one excel file but various sheets.
I'm using bytescout.spreadsheet to work with excel files.
How can I write multiple table sheets to excel file using this tool and C#?
Read the manual: http://bytescout.com/products/developer/spreadsheetsdk/bytescoutxls_working_with_worksheets_in_xls_document.html
using System;
using System.Collections.Generic;
using System.Text;
using Bytescout.Spreadsheet;
namespace Worksheets
{
internal class Program
{
private static void Main(string[] args)
{
// Create new Spreadsheet
Spreadsheet document = new Spreadsheet();
// Add worksheets
Worksheet worksheet1 = document.Workbook.Worksheets.Add("Demo worksheet 1");
Worksheet worksheet2 = document.Workbook.Worksheets.Add("Demo worksheet 2");
// Get worksheet by name
Worksheet worksheetByName = document.Workbook.Worksheets.ByName("Demo worksheet 2");
// Set cell values
worksheet1.Cell(0, 0).Value = "This is Demo worksheet 1";
worksheetByName.Cell(0, 0).Value = "This is Demo worksheet 2";
// Save document
document.SaveAs("Worksheets.xls");
}
}