Adding filters to an Excel document using C1Excel(ComponentOne) and C# - componentone

Can someone show me how to add filters and how to freeze rows in excel using C1Excel(ComponentOne) and C#?
//step 1: create a new workbook
C1XLBook logBook = new C1XLBook();
XLSheet logSheet = logBook.Sheets[0];
I have created XLSheet, but no property to add filters.

You can create frozen rows using Frozen property as :
C1.C1Excel.XLSheet sheet = c1XLBook1.Sheets[0];
sheet.Rows.Frozen = 2;
However, currently creating filters are not supported in C1Excel.
Thanks,
Richa

Related

PhpSpreadSheet how to have a new line?

Good day everyone i want to achieve a new line in my spreadsheet cause i had two to three values here
This code over here has a protection so the viewers cannot edit easily in excell they must have password on the admin to be enable to edit.
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$spreadsheet->getActiveSheet()->getProtection()->setSheet(true);
$spreadsheet->getActiveSheet()->getProtection()->setSort(true);
$spreadsheet->getActiveSheet()->getProtection()->setInsertRows(true);
$spreadsheet->getActiveSheet()->getProtection()->setFormatCells(true);
$spreadsheet->getActiveSheet()->getProtection()->setPassword('test');
$spreadsheet->getActiveSheet()->getStyle('D')->getAlignment()->setWrapText(true);
$spreadsheet->getDefaultStyle()
->getFont()
->setName('Times New Roman')
->setSize(14);
$sheet->setCellValue('A1', 'Control Number');
$sheet->setCellValue('B1', 'Requesting Unit');
$sheet->setCellValue('C1', 'Project Details');
$sheet->setCellValue('D1', 'Attached Transaction Form');
This line of code uses to fetch the data in the database in this scenario my attached form has two to three values so thats why i want to create a break or a new line.
$data = $this->m->getlog1();
$slno = 1;
$start = 2;
foreach($data as $d){
$sheet->setCellValue('A'.$start, $d->control_number);
$sheet->setCellValue('B'.$start, $d->requesting_unit);
$sheet->setCellValue('C'.$start, $d->project_details);
$sheet->setCellValue('D'.$start, $d->attached_form);
$start = $start+1;
$slno = $slno+1;
}
You have to use line breakes in the text and
$spreadsheet->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
For more Information see documentation.
According to this commit this commit this will be automatically included in future releases.
Maybe you must/will use some auto fit then take a look to this code.

Using an existing spreadsheet as a template PHPspreadsheet

Objective
I want to use an existing Excel sheet, as a template to create an invoice.
Cell styling, such as coloring have to be included
An image (logo) has to be included
Standard data such as company address has to be included
I've read something about cfspreadsheet, but I'm not entirely sure how to use it.
Question A:
Is there a way to use a template file? Or do you know any alternatives?
Question B
Is it possible to use $_POST data with this library?
Example
$data = $_POST['example'];
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', '$data');
I am not 100% sure, but according to PhpSpreadsheet's doc, you can read a local file (your pre-made template) with :
$inputFileName = './sampleData/example1.xls';
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
Or in case you already know the file type (.xlsx or .xsl) :
$inputFileType = 'Xls'; // Xlsx - Xml - Ods - Slk - Gnumeric - Csv
$inputFileName = './sampleData/example1.xls';
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
You can also wrap all of that in a try catch if you want.
Then you just have to make changes the same way you would populate a Spreadsheet you created, populating cells with data you get from pretty much where you want with php, examples :
Classic variable $foo = 'bar';
$_GET / $_POST / $_REQUEST
From a Database

MVC5 with ReportViewerForMvc

I am designing an MVC application using ReportViewerForMVC. This is my controller code:
ReportViewer rp = new ReportViewer();
rp.ProcessingMode = ProcessingMode.Local;
rp.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath)
+ #"Report/sampleFile.rdlc";
ViewBag.ReportViewer = rp;
This is my View:
#using ReportViewerForMvc
#Html.ReportViewer(ViewBag.ReportViewerMicrosoft.Reporting.WebForms.ReportViewer)
The iframe shows but i get this message:
A data source instance has not been supplied for the data source 'DataSet1'.
as my output for the report section. I thought i specified my data source when designing my .rdlc file.
Again, i want to ask if i create a datatable with a where clause having parameter, how can i specify the value in my controller.
I have searched online and i'm not getting any useful. Can anyone please help me out?
The report viewer has no DataSource. If you like using designer view like myself, you could start by creating a dataset and adding a dataAdapter which will automatically add a dataTable. The dataSource can be set like this:
DataSet1 ds = new DataSet1();
TableAdapter1 ta = new TableAdapter1();
ta.Fill(ds.Table[0]);
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet";
rds.Value = ds.Table[0];
rp.LocalReport.DataSources.Clear();
rp.LocalReport.DataSources.Add(rds);
rp.LocalReport.Refresh();
As simple as that...
Change the following in View
#Html.ReportViewer(ViewBag.ReportViewerMicrosoft.Reporting.WebForms.ReportViewer)
to
#Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)

how to do custom filtering and custom pagination

I am not using Entity framework, i have created custom Data Access Layer.
i want to do custom filtering and custom pagination.
does grid.mvc supports custom pagination and custom filtering?
i have checked http://gridmvc.codeplex.com/documentation documentation, but can not find how to do it.
i have tried
EnablePaging = true;
Pager.PageSize = 10;
but it will do pagination on already paged data.
if anybody had done it, please suggest how it can be done.
Thanks in advance.
I haven't worked with grid.mvc, but you can do paging and filtering on virtually any sequence in .NET using LINQ.
var items = new List<string>();
// Add 10 items for testing
for (int i = 0; i < 10; i++)
{
items.Add("item" + i.ToString());
}
var pagedAndFilteredItems = items
.Where(x => x != "item0") // Filter the first item from the sequence
.Skip(3) // Number of items to skip over
.Take(3) // Number of items to put into the current view.
.ToList(); // Executes the query
// pagedAndFilteredItems now contains
// "item4"
// "item5"
// "item6"
// Which corresponds to the second page if your page size is 3
You could then only provide 1 page at a time to grid.mvc. You might have to build your own next and back links, but if you are pulling data from an external source, this is far more scalable than providing the entire dataset to the grid.
Of course, another option is to do the filtering in LINQ as shown with the Where clause and then have grid.mvc do the pagination.

EPPlus loses row height

I have to use an existing excel and place contents on specific cells and give it to user through ASP.Net MVC site. The original excel should not be changed, so that it can be used as a template.
The issue is though functionally everything works with EPPlus, the row heights of the original excel is completely lost in the resulting excel.
using (ExcelPackage package = new ExcelPackage(new FileInfo(NewFile), new FileInfo(Template)))
{
ExcelWorksheet excelSheet = package.Workbook.Worksheets[1];
//Do all process, mainly set specific cell values
package.Save();
}
//Return the new file to user
I always create a copy of my file first rather than opening any file that I want to remain unchanged. I would try:
File.Copy(originalTemplate, workingTemplate, true);
using (ExcelPackage package = new ExcelPackage(new FileInfo(NewFile), new FileInfo(workingTemplate)))
{
ExcelWorksheet excelSheet = package.Workbook.Worksheets[1];
//Do all process, mainly set specific cell values
package.Save();
}
//Return the new file to user

Resources