Coder Social home page Coder Social logo

Comments (3)

paulyoder avatar paulyoder commented on July 2, 2024

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.

BenPG avatar BenPG commented on July 2, 2024

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.

Hussain08 avatar Hussain08 commented on July 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.