Excel sheet with XSLT transformation ... need protection - c#

I need to export DataSet result to Excel file (using dataset, XSLT transformation and XML generation => XML spreadsheet). That's work, but I'd like to protect this sheet. The reader can only make change in some dropdown, in some cell but other change are not allowed. The only changes allowed are some specific cells (value or dropdown).
How can I do this ?
Thanks,

You could always lock the cells and protect the sheet with the Excel Interop. Somthing similar to:
sheet.get_Range("A1, N1").Locked = true;
sheet.Protect(....);

For Excel 2003 and above you can develop the solution by saving a sample workbook as XML to provide a template as the basis for your XSLT template. If you protect the worksheet and add your drop downs etc. before saving, you should be able to work out what styles to give each cell in your generated data.
Excel 2003 does not allow the worksheet to be password protected but 2007 may have some encryption magic that gets round this.

Related

Parsing excel sheet - cell by cell

I would like to see your help on how to parse an excel file in mvc c# but, accessing its cell address like:
var a = Excel_cell{A1:B1}
model.a = a
I've looked to various plugin like excelfilereader, epplus and also javascript libraries like sheetjs and alike but, they do the same where they parse the whole file with the layout of a table...
My excel file is quite complicated because it has no headers and its data is on the right side.
EG:
I apologize if I put an image here...
My requirement is read an excel template, and save its data to the database.. I dont need to send the file to server because it will not be beneficial in the long run
Im hoping you could help me... An idea how to accomplish this would be really help.
You can try by using Spire.XLS, It's a good library that helps you read and write (this one only with a license) excel files and some other formats, it's easy to use and intuitive, for example this would be the code to get an excel file and read some cells:
//Create a variable to access the excel file
Workbook yourFile = new Workbook();
Stream stream = new MemoryStream(FilePath);
//Load the excel file into the variable
workbook.LoadFromStream(stream);
//Select the worksheet you want to take values from
var firstSheet = workbook.Worksheets.Where(x => x.Name == "Name of the sheet").First() as Worksheet;
string value = firstsheet.Range[A1].Text;
So with this code you have populated a variable with the excel file, a variable with a specific sheet (for example the first one), and then you save the text value of a specific cell of your choice in another variable, let me know if I got it right
EDIT: This is their site and this is the nuget gallery

How to merge csv and excel?

I am trying to build simple program that does my weekly job.
Everytime I receive csv file, I maintain excel file.
My csv is like below:
key_code,eng_name,...so on
000001,some name,...so on
My excel is like below:
Some text are written on A1-G4
No column hearders written
Data is from 5th row
Each row has data from B-G(1st row B5-G5, 2nd row B6-G6)
If key_code in csv does not exist in excel, I add.
If key_code in csv does exist in excel, I update the rest columns.
If key_code in excel does not exist in csv, I delete the row.
Can anyone tell me any easy way or steps to get this done?
I am very confusing about what to use to update excel file among OleDb, Interop.Excel, epplus, spire.xls, etc.
And in which class do I have to store csv data and excel data to compare.
For reading CSV you can use ChoETL reader, this is one of the best CSV readers I have ever used.
The tricky part is to how to write Excel file and choosing the right tool, amongst the tools you have mentioned EPPlus is best because
Excel.Interop needs Excel(MS Office) to be installed on production machine which can create licencing issues
To use OleDB you need some nitty gritty to use it a better way
EPPlus provides some abstraction which makes it easy to manipulate the excel files
using (var p = new ExcelPackage())
{
//A workbook must have at least on cell, so lets add one...
var ws=p.Workbook.Worksheets.Add("MySheet");
//To set values in the spreadsheet use the Cells indexer.
ws.Cells["A1"].Value = "This is cell A1";
//Save the new workbook. We haven't specified the filename so use the Save as method.
p.SaveAs(new FileInfo(#"c:\workbooks\myworkbook.xlsx"));
}
This is very simple example given on the github page to write, please use it and post any specific issues
If key_code in csv does not exist in excel, I add.
If key_code in csv does exist in excel, I update the rest columns.
If key_code in excel does not exist in csv, I delete the row.
As my understanding of the rules above, you simply delete the old excel file and create a new file from the data in the CSV file.
You can use R to do this very easily:
#Install package 'writexl' if you didn't, by install.packages("writexl")
library(writexl)
#File excel
fn <- "file.xlsx"
#Check its existence
if (file.exists(fn))
#Delete file if it exists
file.remove(fn)
#Read the csv file to a data frame
df <- read.csv("C:/newfile.csv")
#Write the data frame to excel file. Change col_names = TRUE if you want the headers.
write_xlsx(
df,
path = "file.xlsx",
col_names = FALSE
)

Is Open XML SDK 2.5 able to recalculate Excel formulas?

I am struggling with the Open XML SDK and I've already read a lot of posts on this topic but cannot figure it out. My goal is to have a locally created Excel file which contains a formula and edit the input online and retrieve the calculated value online.
I don't know if this is possible since Open XML may only change the data and I wonder if it is also able to perform Excels calculations.
For example, my local file contains three cells:
A1: 1
A2: 2
A3: =(A1+A2)
Using Open XML I adjust A2 to the value of 3, however the result of A3 remains 3 instead of 4.
I have already read about Excel having to recalculate, but my goal is to have an Excel file as some sort of calculation engine instead of transfering all calculations to C#.
All tips and advice are welcome.
Kind regards, Patrick
First of all thanks for all responses.
Second I guess the response answered my question and the open XML SDK is only able to adjust the file and won't do anything regarding recaculating existing formulas in the file. This will only occur when opened in Excel. I will take a look at EPPlus.
You can use something like this.
Cell cell; //supposing this is your cell referencing A3
CellFormula cellformula = new CellFormula();
cellformula.Text = "SUM(A1, A2)";
CellValue cellValue = new CellValue();
cellValue.Text = "0";
cell.Append(cellformula);
cell.Append(cellValue);
A similar example can be found at this link: Formula cells in excel using openXML
I feel there could be some ambiguity captured in the question. OpenXML is way of storing documents, therefore it is not possible to do calculations with OpenXML SDK. It is spread sheet engine (Excel application) which performs the calculations.
When inputs are updated, spreadsheet saved, calculated values should get updated.

Output table from excel using xslt

I would like to output a table to a webpage. The table is stored in an excel sheet (xls).
Is it possible to use xslt for this? The table is the cells are in this range:
A26 - P36 (16 columns and 11 rows)
If an exmaple file is need here is a link:
http://finans.opengate.dk/media/6704/2010-01-13.xls
Update: A daily file is uploaded. And I would like to automatically show a table from the latest xls-file using xslt. If some C# is needed to convert it from excel to something else (XML?) that is fine. It is done in the CMS Umbraco and that is why I hope to use XSLT since that is the way to show things in Umbraco, through xslt makroes.
BR. Anders
UPDATE with answer (based on answers below): No, it is not possible to read xls-files using xslt. If needed then one has to save excel sheet in another format xml or html. Or one will need a real programming language to read the excel file.
XSLT is mostly used to convert XML from one dialect to another, not to convert xls files to html.
If you just want to do this manually, you can save your worksheet as HTML directly in excel.
It is not clear from your question if you want to do this programmatically, and if so using what programming language.
You can use ADO.net to access cells in an excel file, similar to a DB query. This is a bit lighter than trying to use Excel automation objects.
http://support.microsoft.com/kb/316934
SpreadsheetGear for .NET can read Excel files and display them in a DataGrid as shown in the Excel to DataGrid sample on this page:
// Create a workbook from an Excel file
String ssFile = Server.MapPath("files/spiceorder.xls");
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(ssFile);
// Get a DataSet from an existing defined name
DataSet dataSet = workbook.GetDataSet("orderrange", SpreadsheetGear.Data.GetDataFlags.FormattedText);
// Bind a DataGrid to the DataSet
DataGrid1.DataSource = dataSet;
DataGrid1.DataBind();
SpreadsheetGear can also render png/gif/jpg images from cell ranges or charts as demonstrated here.
You can download the free trial here if you want to try it yourself.
Disclaimer: I own SpreadsheetGear LLC

How to store data to Excel from DataSet without going cell-by-cell?

Duplicate of: What’s the simplest way to import a System.Data.DataSet into Excel?
Using c# under VS2008, we can create an excel app, workbook, and then worksheet fine by doing this:
Application excelApp = new Application();
Workbook excelWb = excelApp.Workbooks.Add(template);
Worksheet excelWs = (Worksheet)this.Application.ActiveSheet;
Then we can access each cell by "excelWs.Cells[i,j]" and write/save without problems. However with large numbers of rows/columns, we are expecting a loss in efficiency.
Is there a way to "data bind" from a DataSet object into the worksheet without using the cell-by-cell approach? Most of the methods we have seen at some point revert to the cell-by-cell approach. Thanks for any suggestions.
Why not use ADO.NET using the OLEDB provider?
See: Tips for reading Excel spreadsheets using ADO.NET
This is a duplicate question.
See: What's the simplest way to import a DataSet into Excel
(The answer is, use the OleDbConnection and read the dataset from Excel).
Instead of going cell by cell, it is faster to set a 2-dimensional array of objects to an excel range.
object[][] values = ...
ws.Range("A1:F2000").Value = values;
However, the OleDb way is still much faster than above.
You could also render a GridView to a string and save the results to the client as HTML or XLS. If you use the XLS file extension to associate the data with Excel, you'll see an error when you open the file in Excel. It doesn't hurt anything and your HTML data will open and look perfect in Excel...

Categories

Resources