Comments (3)
Try using the property to column
mappinghttps://github.com/paulyoder/LinqToExcel#property-to-column-mappingfeature.
var excel = new ExcelQueryFactory("excelFileName");
excel.AddMapping(x => x.State, "Providence"); //maps the
"State" property to the "Providence" column
excel.AddMapping("Employees", "Employee Count"); //maps the
"Employees" property to the "Employee Count" column
var indianaCompanies = from c in excel.Worksheet()
where c.State == "IN" && c.Employees > 500
select c;
On Tue, May 29, 2012 at 2:56 AM, danielc777888 <
[email protected]
wrote:
I have a class with property "AVG_BAL", in the excel file the header name
is "AVG_BAL " with a trailing space.
AVG_BAL property on object result is always null. I do no have control
over the file therefore I cannot change the column name.Is there a way for LinqToExcel to ignore any leading or trailing spaces?
Reply to this email directly or view it on GitHub:
#9
from linqtoexcel.
I had this same issue... source software exports Excel with trailing spaces, but if a user saves from excel, no trailing spaces.
Resolved with this:
var worksheetName = excel.GetWorksheetNames().FirstOrDefault();
if(worksheetName != null)
{
//For Some columns, source software exports them with a column name that has a trailing space.
//If these are opened in Excel and then saved, the trailing space is removed
//Ensure the mappings account for both instances.
foreach(var colName in excel.GetColumnNames(worksheetName))
{
switch (colName)
{
case "Vehicle Id ":
excel.AddMapping<Ticket>(t => t.VehicleId, "Vehicle Id ", d => d.ToString());//column in excel file has trailing space
break;
case "Vehicle Id":
excel.AddMapping<Ticket>(t => t.VehicleId, "Vehicle Id", d => d.ToString());
break;
case "Id ":
excel.AddMapping<Ticket>(t => t.VendorId, "Id ");//column in excel file has trailing space
break;
case "Id":
excel.AddMapping<Ticket>(t => t.VendorId, "Id");
break;
default:
continue;
}
}
}
Would be cool if there were an option to trim column names
from linqtoexcel.
Thank you so much guys for such an elegant library!
@danielc777888 This is the workaround I added to circumvent the leading and trailing spaces in the column mapping.
private ExcelQueryFactory MapSpreadsheetColumnsToInvoiceModel(ExcelQueryFactory excel)
{
var columnNames = excel.GetColumnNames(WorkSheetName).ToList();
excel.AddMapping<InvoiceModel>(x => x.Quantity, columnNames.First(x => AreStringsCaseInsensitiveEqual("Quantity", x.Trim())));
excel.AddMapping<InvoiceModel>(x => x.Changes, columnNames.First(x => AreStringsCaseInsensitiveEqual("Changes", x.Trim())));
excel.AddMapping<InvoiceModel>(x => x.VendorNumber, columnNames.First(x => AreStringsCaseInsensitiveEqual("Vendor Number", x.Trim())));
return excel;
}
private bool AreStringsCaseInsensitiveEqual(string val1, string val2)
{
return string.Equals(val1, val2, StringComparison.OrdinalIgnoreCase);
}
from linqtoexcel.
Related Issues (20)
- Numbers returned as formatted value HOT 6
- İmpersonator Connection
- Start WorkSheet cast at desired row
- OleDbException when opening modified spreadsheet HOT 8
- Mapping Unmapped columns HOT 1
- Failure to map columns with square brackets in the header row HOT 1
- Read Zipped data
- Reading file returns only first record and only one property value HOT 1
- The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters...
- .NetCore 3.1 support? HOT 6
- Microsoft Access Database Engine Redistributable 2010 vs 2016 HOT 1
- How to map the merged columns ?
- New nuget package HOT 1
- WorksheetRangeNoHeader returning blank for cells with values
- Hangs on excelFile.GetWorksheetNames();
- DotNet 5 support HOT 2
- "Unspecified Error" when reading multiple files sequentially.
- Will the 'Microsoft.ACE.OLEDB.12.0 problem happen in azure?
- Log4Net dependency HOT 1
- Support for ListObjects (Excel Tables) ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from linqtoexcel.