I've encountered a problem. I'm trying to read data from xlsx document using LinqToExcel. The data is retrieved but the document has cells which are interpreted as formulas with text like "=- Something". Is it possible to get those cells text and not the counted value of the formula?
var book = new LinqToExcel.ExcelQueryFactory(path);
var worksheetNames = book.GetWorksheetNames();
var query =
from row in book.WorksheetNoHeader()
where row[0] != ""
select row;
With LinqToExcel you can't receive formula, because linqtoExcel uses OleDB to access sheet. OleDB doesn't allow to access formulas. Instead linqtoExcel you can use Worksheet.Range Property
Example:
Range range = (Range)sheet.Cells[i, j];
return range.Formula;
Related
I'm working with DataGridView and excel interop. I have access file where are column date and column time. With SQL I create new column DateTime which I then show in dgv.
SELECT
FORMAT(DateOS, 'dd.MM.yyyy') AS DateOS,
FORMAT(TimeOS, 'hh:mm:ss') AS TimeOS,
FORMAT(DateOS+TimeOS, 'dd.MM.yyyy HH:mm:ss') AS DateTimeOS
FROM [table]
Then I select whole column DateTimeOS and copy it to clipboard.
After this I PasteSpecial to Excel file which has prepared format of column.
using Excel = Microsoft.Office.Interop.Excel;
Excel.Workbook excelExportFile;
Excel.Worksheet wsExport;
wsExport = excelExportFile.Worksheets["Data"];
dviewAll.ColumnHeadersVisible = false;
dviewAll.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
dviewAll.Columns[columnNameDateTime].Selected = true;
DataObject data = dviewAll.GetClipboardContent();
if(data != null)
Clipboard.SetDataObject(data, true);
dviewAll.ColumnHeadersVisible = true;
wsExport.Cells[2, 2].PasteSpecial();
Result is first cell is formated as string, rest is formated as datetime.
picture
When I open this string cell and just press enter, it will format to datetime as rest.
Any ideas, how to prevent this issue, or how to programically open cell and press enter?
UPDATE:
First suggestion from Excel treating date column as general when exporting to Excel using C# gives me exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'The name
'Format' is bound to a method and cannot be used like a property'
And for the second one: Excel.Worksheet does not have "Column" property.
I have a vsto application that populates an Excel sheet.
First I populate the datatable. The next function is to create a vsto listobject:
// Populate the sheet
XlsInterop.Excel.Worksheet worksheet = (XlsInterop.Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets.Add();
extendedWorksheet = Globals.Factory.GetVstoObject(worksheet);
XlsInterop.Excel.Range cell = extendedWorksheet.Range["A1"];
excelItemsList = extendedWorksheet.Controls.AddListObject(cell, sheetName);
excelItemsList.AutoSetDataBoundColumnHeaders = true;
excelItemsList.DataSource = dt;
excelItemsList.ListColumns[5].Range.NumberFormat = "#";
Now the problem is that in column "5" I have values like 005, 004. In Excel, the values display as 5 and 4. How can I make sure that the values in Excel show the same values as in my database?
If I change my code like:
excelItemsList.SetDataBinding(dt);
excelItemsList.ListColumns[5].Range.NumberFormat = "#";
excelItemsList.SetDataBinding(dt);
It is doing what I want, but I don't think that this is the way to go.
If you know how many leading zeros you need, you can then format the column with that.
.NumberFormat = "000";
The column would then still be evaluated as a number.
I have excel template with empty one-column table. I need to populate it with some string values (this is needed for setting lookups using data validation, but I guess it doesn't really matter)
I came up to getting Table object and I assume I should use Append method
var workBookPart = doc.WorkbookPart;
var lookupsSheet = (Sheet)workBookPart.Workbook.Sheets.FirstOrDefault(x => (x is Sheet && ((Sheet)x).Name == "Lookups"));
var worksheetPart = (WorksheetPart)workBookPart.GetPartById(lookupsSheet.Id);
var table = worksheetPart.TableDefinitionParts.FirstOrDefault(x => x.Table.DisplayName == "ValuesTable")?.Table;
Can someone enlighten about the correct way of adding rows to such table. Thanks!
I would suggest you use the ClosedXML library to set the values of cells in your worksheet. By using ClosedXML, you will be able to populate the cells you want in the following fashion:
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Demo");
// Set the values for the cells
ws.Cell(1, 1).Value = "Value";
ws.Cell(2, 1).Value = 1;
ws.Cell(3, 1).Value = 2;
ws.Cell(4, 1).Value = 3;
ws.Cell(5, 1).Value = true;
Note that you can set the value of a cell to a string, an integer, and a boolean without doing any explicit casting. You can set the value of a cell without doing any explicit casting to other types as well, as it is explained in the following link: Cell Values.
For more information regarding the ClosedXML library please refer to the documentation.
As a side note, I was really eager to use Open XML to manipulate Excel spreadsheets but I found ClosedXML way easier to use.
Please help me to fetch or to read specific cell data of .xls spreed sheet using GemBox in c#.
I am able to write but fails to read the specific cell data.
I don't want to read all data at once.
var dataSet = new DataSet();
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
GemBox.Spreadsheet.ExcelFile ef = new GemBox.Spreadsheet.ExcelFile();
// ExcelFile ef = ExcelFile.Load(#"C:\Users\LENOVO\Documents\100sitesSpreedSheet.xls");
ef = GemBox.Spreadsheet.ExcelFile.Load(#"C:\Users\LENOVO\Documents\100sitesSpreedSheet.xls");
ExcelWorksheet ws = ef.Worksheets["100sitesSpreadSheet"];
ws.Cells[6, 0].Value = "abcd";
ef.Save(#"C:\Users\LENOVO\Documents\100sitesSpreedSheet.xls");
to read the specific cell's value you can just use the Value property, like the following:
Console.WriteLine(ws.Cells["A7"].Value);
This property is of an object type and can return a text or a number or a date, depending on the data it stores. Also note that GetFormattedValue method actually converts that value into a string representation by using a number format which is applied to that cell.
I'm trying to read data from a multiple sheet excel file.
The first sheet has column headers. the second sheet does not. and the third one does not have any. I can successfully read from the first sheet.
When i try reading from the sheet without header, the program throughs an exception.
var sheet1_data = from c in excel.Worksheet<Species>("Sheet1") select c; // This works and the question is how do i retreive data from sheet2?
I'm currently using this:
var Sheet2_data = from b in excel.WorksheetNoHeader("sheet2") select b;
When i try and read from sheet2_data, i get the following exception:
'sheet2' is not a valid worksheet name. Valid worksheet names are: 'sheet2,sheet3',
Any help will be highly appreciated.
B.
the sheet name can be abstract using GetWorksheetNames() method.
var sheets = excel.GetWorksheetNames();
foreach(var sheet in sheets)
{
var sheetData = from x in excel.Worksheet(sheet) select x;
}