简体   繁体   English

只在linq中输入值到特定位置的Excel表格才能在c#中表现出色

[英]Entering value into Excel sheet at particular location only thru linq to excel in c#

My Excel sheet is as below: 我的Excel表格如下:

Id   Name  Status
1    XYZ
2    ABC
3    BB
1    Yz

What I am trying to do is to write Checked in the status column whose Id is 1 from my C# application. 我想要做的是在我的C#应用​​程序中写入Id为1的状态列中的Checked

I thought of doing this through Linq to Excel. 我想通过Linq到Excel做到这一点。 But I am not getting anything positive in this direction. 但我在这个方向上没有得到任何积极的结果。

Can anyone guide me properly. 任何人都可以正确指导我。

Thanks. 谢谢。

You are out of luck here, as Linq-To-Excel does not write to spreadsheets ( reference ). 你在这里运气不好,因为Linq-To-Excel没有写入电子表格( 参考 )。 The only option you have is to strongly type your queries, modifiy the results in memory and save them as a .csv to your harddisk - or use another 3rd party library to write the changes back to the excel file. 您唯一的选择是强烈键入查询,在内存中修改结果并将其作为.csv保存到硬盘 - 或使用其他第三方库将更改写回excel文件。

public class Demo
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

string pathToExcelFile = @"path\to\file.xslx";  
string sheetName = "Tabelle1";

var excelFile = new ExcelQueryFactory(pathToExcelFile);
    //strongly type the result
Demo rowToUpdate = (from xc in excelFile.Worksheet<Demo>(sheetName) 
                   where xc.ID == 1
                   select xc).FirstOrDefault();
    //update retrieved record
rowToUpdate.Status = "Active";
    //output in linqpad
rowToUpdate.Dump();
    //no submitchanges or save method available :(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM