How to copy conditional formatting from one sheet (XSSF) to another (SXSSF) via apache POI? - formatting

XSSFWorkbook existingWorkbook = (XSSFWorkbook) WorkbookFactory.create(new File("c:/test1.xlsx"));
XSSFWorkbook brandNewWorkbook = (XSSFWorkbook) WorkbookFactory.create(true);
XSSFSheet existingSheet = existingWorkbook.getSheet("Sheet1");
CTWorksheet ctWorksheet = existingSheet.getCTWorksheet();
List<CTConditionalFormatting> formattingList = ctWorksheet.getConditionalFormattingList();
XSSFSheet sheet = brandNewWorkbook.createSheet("Sheet1");
for (CTConditionalFormatting cf : formattingList) {
CTConditionalFormatting newCf = sheet.getCTWorksheet().addNewConditionalFormatting();
newCf.xsetSqref(cf.xgetSqref());
newCf.setCfRuleArray(cf.getCfRuleList().toArray(new CTCfRule[]{}));
}
CTDxfs dxfs = existingWorkbook.getStylesSource().getCTStylesheet().getDxfs();
brandNewWorkbook.getStylesSource().getCTStylesheet().setDxfs(dxfs);
try (FileOutputStream out = new FileOutputStream(new File("c:/test2.xlsx"))) {
brandNewWorkbook.write(out);
}
This was the answer of Andrey Grigoriev on how to copy XSSF Sheet conditional formatting to XSSF Sheet conditional formatting.
Now I would need pretty much the same. But from XSSF to SXSSF (Streaming API).
We cannot get rid of the streaming api because of memory issues, so we can`t use the solution above.
I can`t even find a method on the sxssf Sheet to .add() or .set() a conditional style to it. Is this supported? There is only a getSheetConditionalFormatting() method.

Related

Does EPPlus support MS Office Equations?

I am interested in using EPPlus for generating some xslx report files for a system. I'm interested in whether or not it is possible for EPPlus to tap into Excel's Equation's feature. This is different from formula's. I am aware of EPPlus's ability to add formula's and that's a great feature I am also using. The Equation feature in Excel prints out equations in a nice looking format and I am interested in using that in files I generate with EPPlus.
public ActionResult OnPostDownloadExcelFile()
{
var ms = new MemoryStream();
using (var package = new OfficeOpenXml.ExcelPackage(ms))
{
package.Workbook.Properties.Title = "Excel file with an equation in it";
// Worksheet
var worksheet = package.Workbook.Worksheets.Add("Report");
worksheet.View.ShowGridLines = true;
int row = 1;
int col = 1;
worksheet.Cells[row, col].Value = "I want to put an equation here...";
}
return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ExcelFileExample.xlsx");
}
Unfortunately this is not supported in EPPlus. Equations are an embedded object. Embedded objects are not presently supported: https://github.com/JanKallman/EPPlus/wiki/FAQ#what-is-not-supported-by-the-library-these-are-the-most-obvious-features

working with excel files in uwp

my question is - I can't read data from excel file. I'm using spreadsheetgear library. I'm writing for uwp platfom (I need to show user data from excel file). may someone help?
(gv1 = gridview). or I should use other library? May be anyone already worked with spreadsheetgear and can help.
Public Async Function open_excel() As Task
Dim Values As String()
Dim filepicker As New FileOpenPicker With {.ViewMode = PickerViewMode.List, .SuggestedStartLocation = PickerLocationId.Downloads}
filepicker.FileTypeFilter.Add(".xls")
filepicker.FileTypeFilter.Add(".xlsx")
Dim storefile As StorageFile = Await filepicker.PickSingleFileAsync
Dim workbook As IWorkbook = Factory.GetWorkbook(storefile.Path)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
gv1.ItemsSource = worksheet.Cells
From spreadsheetgear document, if you want to open Excel from a file you need to create workbookSet then invoke OpenFromStream method. I have tested, it works in my side.
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
using (var steam = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var workbookSet = Factory.GetWorkbookSet(CultureInfo.CurrentUICulture);
var workbook = workbookSet.Workbooks.OpenFromStream(steam.AsStream());
var worksheet = workbook.Worksheets[0];
var Used = worksheet.UsedRange;
var items = Used.Value;
}
}
else
{
return;
}
I have a question, I use the code to open the excel, and if I want to save it, what is it the code?.
I use the code of open the excel. If you dont want open a windows dialog, and you prefer to give de path directly, you can use the following code:
var folder = await StorageFolder.GetFolderFromPathAsync(#"C:\Excel_Modelo");
var file = await folder.GetFileAsync("Modelo.xlsx");
if (file != null)
{
var workbookSet = Factory.GetWorkbookSet(CultureInfo.CurrentUICulture);
IWorkbook workbook = workbookSet.Workbooks.OpenFromStream(steam.AsStream());
var worksheet = workbook.Worksheets[0];
var Used = worksheet.UsedRange;
var items = Used.Value;
}

Using IF condition to populate a new sheet in Google App Script Spreadsheet

I have a Google Spreadsheet containing 2 sheets.
If the content of a specific cell in sheet 1 is equal to something, I want to write something in a specific cell of the sheet 2.
As I need to do it a lot of times, I want to do it using Google App Script. Using VBA, this is super easy, but here it returns me that I "can't change or modify the properties of the object". Do you have any idea of what's wrong with my code?
Thank you for your help !
Here is my code :
function synthese() {
var results = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('results');
var synthese = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('synt');
B3 = results.getRange("B3");
if (B3="Yes") {
results.getRange("A1").setValue = "Yes";
} else {
results.getRange("A1").setValue = "No";
}
}
B3 is still a range, with properties such as size, background color, etc.
It is not a value, you can extract that via getValue().
Your second problem is that you are doing an assignment in the if.
Lastly setValue is a method not an attribute, so the value needs to be supplied as an argument.
function synthese() {
var results = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('results');
var synthese = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('synt');
var B3 = results.getRange("B3"); // good practice to make variables local
if (B3.getValue() === "Yes") {
results.getRange("A1").setValue("Yes");
} else {
results.getRange("A1").setValue("No");
}
}

How to sort through a dictionary (a real world dictionary) that is in a .csv file?

I haven't read enough theory or have had enough practice in CS, but there must be a simpler, faster way to look up data from a file. I'm working with a literal, real world dictionary .csv file, and I'm wondering how I can speed up look up of every word. No doubt going through the whole list for the word does not make sense; splitting the file into a-z order, and only looking there for each word, makes sense.
But what else? Should I learn SQL or something and try to convert the text database into an SQL database? Are there methods in SQL that would enable me to do what I wish? Please give me ideas!
SQLite sounds fit to this task.
Create a table, import your csv file, create an index and you're done.
I just did this using interop with a moderate size .csv file given me by a supply company. It worked well, but still requires a considerable delay due to the cumbersome decorators used in interop/COM.
class Excel
{
private excel.Application application;
private excel.Workbook excelWorkBook;
protected const string WORD_POSITION = "A"; //whichever column the word is located in when loaded on Excel spreadsheet.
protected const string DEFINITION_POSITION = "B"; // whichever column the definition is loaded into on Excel spreadsheet.
Dictionary<string,string> myDictionary = new Dicationary<string,string>();
public Excel(string path) // where path is the fileName
{
try
{
application = new excel.Application();
excelWorkBook = application.Workbooks.Add(path);
int row = 1;
while (application.Cells[++row, WORD_POSITION].Value != null)
{
myDictionary[GetValue(row, WORD_POSITION)] = GetValue(row, DEFINITION_POSITION);
});
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
excelWorkBook.Close();
application.Quit();
}
}
private string GetValue(int row, string columnName)
{
string returnValue = String.Empty;
returnValue = application.Cells[row, columnName].Value2;
if (returnValue == null) return string.Empty;
return returnValue;
}
}
}
Create a new sql database, import the cab into a new table, place an index on the column that stores the word values, then search against table... That is the approach I would take

Related on Custom Timer Job in sharepoint 2010

How to create a custom job to export an Excel file in a list which has only 2 columns(Title,Description) in a sharepoint 2010?i want the coding part of this question?
Reading Data from Excel and writing into a sharepoint list,this has to be done through custom job coding
Thanks in Advance...
Naresh
Use OpenXMLSDK - a free download that needs to be installed on the server.
[...]
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
public class OffBookAssetLibraryEventReceiver : SPItemEventReceiver
{
public override void ItemUpdated(SPItemEventProperties properties)
{
// This if statement is to work around the sharepoint issue of this event firing twice.
if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
{
byte[] workSheetByteArray = properties.ListItem.File.OpenBinary();
Stream stream = new MemoryStream(workSheetByteArray);
Package spreadsheetPackage = Package.Open(stream, FileMode.Open, FileAccess.ReadWrite);
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(spreadsheetPackage);
SharedStringTablePart shareStringTablePart = spreadsheetDocument.WorkbookPart.SharedStringTablePart;
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
try
{
foreach (Sheet sheet in sheets)
{
var worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id.Value);
IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
if (rows.Count() > 0)
{
int rowNumber = 0;
foreach (Row row in rows)
{
IEnumerable<Cell> cells = row.Elements<Cell>();
Cell title = null;
Cell description = null;
title = cells.ToArray()[0];
description = cells.ToArray()[1];
// This is the code used to extract cells from excel that are NOT inline (Inline cells are decimal and dates - although dates are stored as int)
int index = int.Parse(title.CellValue.Text);
string titleString = spreadsheetDocument.WorkbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(index).InnerText;
index = int.Parse(description.CellValue.Text);
string descriptionString = spreadsheetDocument.WorkbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(index).InnerText;
//Insert into your sharepoint list here!
}
}
}
}
}
}
}
I recommend putting this code into an event receiver on the document library (as seen above).
Have you looked at Excel REader for .NET
http://exceldatareader.codeplex.com/
Open the Excel File
Take a look at the Excel Services for SharePoint 2010. There is a walkthrough explaining the required steps to open an excel file.
SharePoint Custom Timer Job
To create a custom SharePoint timer job, you have to create a class that inherits from SPJobDefinition. A complete tutorial can be found in this blog post: Creating Custom Timer Job in SharePoint 2010.