donnytian / Npoi.Mapper

Use this tool to import or export data with Excel file. The tool is a convention based mapper between strong typed object and Excel data via NPOI.
MIT License
589 stars 114 forks source link

Put(fileName, sheetName, true) does not overwrite the sheet #68

Closed xjfnet closed 2 years ago

xjfnet commented 4 years ago
var mapper = new Mapper(fileName);
var list = mapper.Task<ClassT>(sheetName).Select(x => x.Value).ToList();
list.ForEach(x => x.Name = "1111111");
mapper.Put(list, sheetName);
mapper.Save(fileName);

then open the xlsx file, i found the sheet unchanged, new value does not overwirte the old one but a year ago, these code work's fine

xjfnet commented 4 years ago

the file has 26 sheets, the sheet has 585 rows and 123 columns

xjfnet commented 4 years ago
void Main()
{
    var students = new List<Student>();
    students.Add(new Student() { Name = "aaa", Age = 1 });
    students.Add(new Student() { Name = "aaa", Age = 1 });
    students.Add(new Student() { Name = "aaa", Age = 1 });

    var file = "test.xlsx";
    if (false)
    {
        var mapper = new Mapper();
        mapper.Put(students);
        mapper.Save(file);
    }

    if (true)
    {
        var mapper = new Mapper(file);
        var list = mapper.Take<Student>().Select(x => x.Value).ToList();
        list.ForEach(x => x.Age = 4);
        mapper.Put(list);
        mapper.Save(file);
    }
}

public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}

this work's fine, the sheet was overwritten

xjfnet commented 4 years ago

but for the big xlsx file, i must remove the sheet first, then Put data, like these

var mapper = new Mapper(fileName);
var list = mapper.Task<ClassT>(sheetName).Select(x => x.Value).ToList();
list.ForEach(x => x.Name = "1111111");
mapper.Workbook.RemoveSheetAt(mapper.Workbook.GetSheetIndex(sheetName));
mapper.Put(list, sheetName);
mapper.Save(fileName);
donnytian commented 3 years ago

I've tested with multi-sheet and multi-row file, seem works fine. Please pay attention on the Put method, you may put into another sheet, if you do not specify sheet name or index it will be the first sheet.

xjfnet commented 3 years ago

i wirte the program last year, and it work's fine then i update Npoi.Mapper recently, and found the Sheet can not be overwritten i have to remove the sheet first, then put the sheet with the same name and save.

donnytian commented 3 years ago

can you upload the file and your code snippet?

donnytian commented 2 years ago

closing stale issues