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");
Related
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();
}
I have an ASP.NET Web API and need to implement this feature for a future version. Specifically:
Several .xls files will be placed in a temporary folder. They have the same width but different heights, as well as different row and column sizes.
The files need to be appended into one .xls file
The final excel cannot have multiple worksheets.
The Office Interop libraries cannot be used because Office is not installed and cannot be instaled on the deployment environment.
Is there a way to do this without using the office interop libraries as specified, or any paid third party libraries (free third party libraries are more than welcome) ?
Install Spire.XLS from NuGet: Install-Package Spire.XLS -Version 9.6.7
Spire.XLS provides two ways to merge excel files into a single excel worksheet:
1. Merge excel with styles using CellRange.Copy()
static void Main(string[] args)
{
string outputPath = "‪output.xls";
List<string> files = new List<string>();
files.Add(#"File1.xls");
files.Add(#"File2.xls");
CombineFiles(files, outputPath);
}
private static void CombineFiles(List<string> files, string outputPath)
{
Spire.Xls.Workbook resultworkbook = new Spire.Xls.Workbook();
resultworkbook.Worksheets.Clear();
Spire.Xls.Worksheet resultworksheet = resultworkbook.Worksheets.Add("worksheet");
Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
for (int i = 0; i < files.Count; i++)
{
workbook.LoadFromFile(files[i]);
Worksheet sheet = workbook.Worksheets[0];
if (i == 0)
{
sheet.AllocatedRange.Copy(resultworksheet.Range[1, 1], true, true);
}
else
{
sheet.AllocatedRange.Copy(resultworksheet.Range[resultworksheet.LastRow + 1, 1], true, true);
}
}
resultworkbook.SaveToFile(outputPath, ExcelVersion.Version97to2003);
}
Reference: How to merge multiple worksheets to a single worksheet with styles
2. Merge excel without styles using DataTable
Workbook workbook1 = new Workbook();
//load the first workbook
workbook1.LoadFromFile(FilePath1);
//load the second workbook
Workbook workbook2 = new Workbook();
workbook2.LoadFromFile(FilePath2);
//load the third workbook
Workbook workbook3 = new Workbook();
workbook3.LoadFromFile(FilePath3);
//import the second and third workbook's first worksheet into the first workbook using datatable
Worksheet sheet1 = workbook1.Worksheets[0];
Worksheet sheet2 = workbook2.Worksheets[0];
Worksheet sheet3 = workbook3.Worksheets[0];
DataTable dataTable1 = sheet2.ExportDataTable();
DataTable dataTable2 = sheet3.ExportDataTable();
sheet1.InsertDataTable(dataTable1, false, sheet1.LastRow + 1, 1);
sheet1.InsertDataTable(dataTable2, false, sheet1.LastRow + 1, 1);
workbook1.SaveToFile(OutputPath + "Merged.xls", ExcelVersion.Version97to2003);
Reference: How to merge 3 Sheets from different Excel files into one sheet with C#
Using EPPlus I want to add a new sheet to an Excel file but I do not want to delete the existing sheets in the file if any, and I want to insert it as the first sheet in the file.
Here is what I have written for a quick test but it deletes all the existing sheets:
using (ExcelPackage p = new ExcelPackage())
{
p.Workbook.Worksheets.Add("HubaHuba");
p.Workbook.Worksheets.MoveToStart("HubaHuba");
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = "HubaHuba";
var cell = ws.Cells[1, 1];
cell.Value = "dfsdfsdfsd";
cell = ws.Cells[1, 2];
cell.Value = "347895y5 Oh";
Byte[] bin = p.GetAsByteArray();
File.WriteAllBytes(path,bin);
}
using (ExcelPackage excelEngine = new ExcelPackage())
{
excelEngine.Workbook.Worksheets.Add("sheet1");
excelEngine.Workbook.Worksheets.Add("sheet2");
excelEngine.Workbook.Worksheets.Add("sheet3");
String myFile= "c:\....\xx.xlsx";
excelEngine.SaveAs();
}
When you use Add the sheet is added at the and of current file sheets.
If you want to add in a specific position use this function:
excelEngine.Workbook.Worksheets.Add("sheet0");
excelEngine.Workbook.Worksheets.MoveBefore(4, 1);
sheet0 is added at the and in 4th position and you move it in first position with the previous code.
That's because you're rewriting the file with command File.WriteAllBytes. Instead you should just call p.Save() and ExcelPackage needs to use the constructor that accepts the file path. Then it will work.
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");
}
}
I am writing a program in C# for analysing a structure details in an earth quake.I have a lot of data and want to put them in a excel file.
I mean i need a function like this :
public void excel(int sheet_no,int row,int column,string value)
Is there any way to parse the text file and Put this data in Excel sheet ?
use this :
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Workbooks workbooks;
_Workbook workbook;
_Workbook workbook2;
Sheets sheets;
_Worksheet worksheet;
app.Visible = false;
workbooks = app.Workbooks;
workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
sheets = workbook.Worksheets;
worksheet = (_Worksheet)sheets.get_Item(1);
worksheet.Cells[row, column] = value;
workbook.Saved = true;
workbook.SaveAs(output_file);
app.UserControl = false;
app.Quit();
Take a look at ClosedXml
Code sample:
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");
But if you are dealing with a lot of data, consider using a database like sqlserver.
It provides a lot of analysis tools.
You can connect it as a datasource to excel too.
For now, I am providing links for you to read # Excel Tasks and code samples # Excel controls in ASP.net. If you're still confused, I may be able to find code samples from my projects or the Internet, which are not hard to find.
Look at link Workbook Open (create) method to create Workbooks, and link Workbook Open method to open Workbooks.