I want to create a table in excel file using XSSFTable, after many research I can not do. The resources for 'NPOI in c#' on internet are very unique.
XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("Architecture");
XSSFTable table = sheet.CreateTable();
CT_Table cttable = table.GetCTTable();
cttable.displayName = "Table1";
cttable.id = 1;
cttable.name = "Test";
// cttable.setRef("A1:C11");
cttable.totalsRowShown = false;
that's what I found but I cannot complete it to insert data inside table .
Related
In C# I have populated an excel file with data. Now I would like to create a table starting at cell A2.
I am using the code below to create the table but instead of creating the table starting at Cell A2, the table is being created starting at cell A3
using var package = new ExcelPackage(file);
var ws = package.Workbook.Worksheets.Add(Name: "MainReport");
var range = ws.Cells[Address: "A2"].LoadFromCollection(people, PrintHeaders: true);
range.AutoFitColumns();
//Formats Header row
ws.Cells[Address: "A1"].Value = "My Data!";
ws.Cells[Address: "A1:C1"].Merge = true;
ws.Column(col: 1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
ws.Row(row: 1).Style.Font.Size = 24;
ws.Row(row: 1).Style.Font.Color.SetColor(Color.Pink);
ws.Row(row: 2).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
ws.Row(row: 2).Style.Font.Bold = true;
ws.Column(col: 3).Width = 20;
//create a range for the table
ExcelRange range_table = ws.Cells[2, 1, ws.Dimension.End.Row, ws.Dimension.End.Column];
//add a table to the range
ExcelTable tab = ws.Tables.Add(range_table, "Table1");
//format the table
tab.TableStyle = TableStyles.Medium2;
Excel itself works so that if you select A2:C4 and create a table and you say "has no Headers" it will put some generic headers in A2:C2 and shift the data area (and the data if there is any) to A2:C5.
If you say "has Headers" the headers will be defined in A2:C2, too, but the data only is in A3:C4.
The real Excel obviously does some guessing whether headers are present or not and pre-ticks the box for you.
As stated here in the API Documentation, there is no flag to define if headers are present in the range so there might be no guessing and it might be assumed it is "no" and the shifting as explained on top takes place.
If you take that into consideration you should be able to work it out.
I am using EPPLUS library for creating excel file with a data table.
I have such column in my stored procedure: emp_name, sales, INR, marketing, INR
But in excel file it generates column name like with column name like emp_name, sales, INR, marketing, INR1 etc.
How do I prevent that to print whatever column name in the data table or any other configurations that I am missing?
Code:
string BasePath = ConfigurationManager.AppSettings["BasePath"];
string fileName = Guid.NewGuid().ToString() + ".xlsx";
string FilePath = BasePath + fileName;
using (ExcelPackage excel = new ExcelPackage())
{
ExcelWorksheet ws =
excel.Workbook.Worksheets.Add("WorkingReport");
ws.Cells["A1"].LoadFromDataTable(data,false);
ws.Row(1).Style.Font.Bold = true;
ws.Column(1).Width = 10;
ws.Column(2).Width = 20;
ws.Column(3).Width = 20;
ws.Column(4).Width = 20;
FileInfo excelFile = new FileInfo(FilePath);
ws.Protection.IsProtected = false;
excel.SaveAs(excelFile);
}
INR and INR1 is printed because your datatable has those columns. When same column is selected more than one time in a select list, SQL automatically aliases the column name by adding number at its end to distinguish between two columns. A datatable cannot have duplicate column names. In your case, second INR is being renamed to INR1. You could try
ws.Cells["A1"].LoadFromDataTable(data,true); //"PrintHeaders" true instead of false
but I think you will get the same result.
If you can somehow manage to get cell address of header after data is loaded you can over write the value as
ws.Cells["A5"].Value="INR"
will overwrite the "INR1".
There are other ways also https://github.com/JanKallman/EPPlus/wiki/Addressing-a-worksheet
I am writing an application in which I need to store data cell values into excel sheet. Everything is working fine but the problem is everytime I run the application, it overwrites the existing data.
So far the code I have taken from Github:
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = this.textBox1.Text;
worksheet.Cell("B1").Value = this.textBox2.Text;
worksheet.Cell("C1").Value = this.textBox3.Text;
worksheet.Cell("D1").Value = col1;
worksheet.Cell("E1").Value = col2;
worksheet.Cell("F1").Value = this.textBox6.Text;
workbook.SaveAs("HelloWorld.xlsx");
Note: I don't want to save data using datatable or anything. I just want to get values from textboxes and append them to the existing sheet. I have visited many stackoverflow post but they doesn't helped me much.
Thanks in advance!
Hope this helps:
var wb = new XLWorkbook("Path to file");
IXLWorksheet Worksheet = wb.Worksheet("Tab name");
int NumberOfLastRow = Worksheet.LastRowUsed().RowNumber();
IXLCell CellForNewData = Worksheet.Cell(NumberOfLastRow + 1, 1);
CellForNewData.InsertData(your_data);
You are explicitly storing the values in row 1 of the spreadsheet. If you want to append the values, you'll have to increment the row number and store the values in the appropriate cells.
I'm having an issue with a very basic thing. Deleting a table using EPPlus.
The problem that I'm facing is that from the code point of view (I've debugged the code, and in the Worksheet.Tables collection, the tabled that I called Delete on is gone. I proceed to save the excel and when I try to open it, Excel tells me that "they" found a problem with it, and if I want them to recover as much as possible. If I select Yes (because No closes the file) the table that I just delete through code is back again.
If I set the ClearRange property in the Delete() method to true, the table is still in the spreadsheet, but this time the header columns are gone, and instead Excel recreates my table with some default names (Column1, Column2, etc, depending on the number of columns the original table had).
I've tried to manipulate the TableXml property of the table, and it's still not working. Other than that, I've just read the Internet for about 3 hours with no apparent solution to my problem.
So, if anyone is wondering, the code for this is:
using (var package = new ExcelPackage(fileToWriteTo, templateToStartWith))
{
var workbook = package.Workbook;
var worksheet = workbook.Worksheets[WORKSHEET_NAME];
worksheet.Tables.Delete("dynamicTable", true);
//worksheet.Tables.Delete("dynamicTable");
package.Save();
}
As you can see, a very basic operation that kinda fails.
If anyone knows of a solution to remove a table from a spreadsheet with EPPlus, please share. Would be much appreciated.
I have trouble with the Delete method as well. I can't even compile when I reference it half the time. Strange.
What if you delete the reference to the table via xml. If it is the only table in the ws or you know the exact order it is pretty easy to do:
[TestMethod]
public void Table_Delete_Test()
{
//http://stackoverflow.com/questions/36359047/unable-to-delete-table-using-epplus
//Throw in some data
var datatable = new DataTable("tblData");
datatable.Columns.AddRange(new[]
{
new DataColumn("Col1", typeof (int)), new DataColumn("Col2", typeof (int)), new DataColumn("Col3", typeof (object))
});
for (var i = 0; i < 10; i++)
{
var row = datatable.NewRow();
row[0] = i; row[1] = i * 10; row[2] = Path.GetRandomFileName();
datatable.Rows.Add(row);
}
var fi = new FileInfo(#"c:\temp\Table_Delete_Test.xlsx");
if (fi.Exists)
fi.Delete();
//Create a worksheet with tables to later open and look for tables (note that EPPlus does not create the table objects until save)
using (var pck = new ExcelPackage(fi))
{
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.Add("source");
worksheet.Cells["A1"].LoadFromDataTable(datatable, true);
worksheet.Cells["A12"].LoadFromDataTable(datatable, true);
var table1 = worksheet.Tables.Add(worksheet.Cells["A1:C11"], "TableTest1");
var table2 = worksheet.Tables.Add(worksheet.Cells["A12:C22"], "TableTest2");
pck.Save();
}
//Open the test workbook and delete the second table
using (var pck = new ExcelPackage(fi))
{
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.First();
var wsXml = worksheet.WorksheetXml;
var nsm = new XmlNamespaceManager(wsXml.NameTable);
var nsuri = wsXml.DocumentElement.NamespaceURI;
nsm.AddNamespace("m", nsuri);
//Remove reference to the second table which should be last
var tablePartsNode = wsXml.SelectSingleNode("m:worksheet/m:tableParts", nsm);
tablePartsNode.RemoveChild(tablePartsNode.LastChild);
pck.Save();
}
}
So it goes from this:
to this:
But if you need to do it by Table.Name that is not so easy. You would have to get the RelationshipID which Epplus has marked as internal. The RID is based on the zip package so to get that you either fork and modify Epplus to expose it or you open the XLSX as a ZipArchive and get the RID from that - kind of a pain.
What is the C# code to Display Excel Pivot Table layout in Tabular Form?
Here is the code from the Macro that I need converted to C#:
ActiveSheet.PivotTables("PivotTable1").RowAxisLayout xlTabularRow
Here is my starting code to show what variables I have set up:
//Excel.Application excel = new Excel.Application();
activeWorkBook = excel.ActiveWorkbook;
sheets = excel.Sheets;
pivotWorkSheet = (Excel.Worksheet)sheets.Add();
// Create the Pivot Table
pivotCaches = activeWorkBook.PivotCaches();
pivotCache = pivotCaches.Create(Excel.XlPivotTableSourceType.xlDatabase,"Sheet1!$A$2:$J$35");
pivotTable = pivotCache.CreatePivotTable("Sheet2!R2C1");
// Set the Pivot Fields
pivotFields = (Excel.PivotFields)pivotTable.PivotFields();
I figured it out in case anyone else is looking for this I used:
pivotTable.RowAxisLayout(Excel.XlLayoutRowType.xlTabularRow);