setcellvalue in excel not updating the file

28 Views Asked by At

I am using NPOI and could not make it set value work for me. I am using 1 stream for both my read and write but could not make it to save into disk . Please help

using (var fs = new FileStream(v, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
    IWorkbook wb = new XSSFWorkbook(fs);

    ISheet sheet = wb.GetSheetAt(0);

    if (v1 == "Read")
    {
        if (sheet != null)
        {
            int rowCount = sheet.LastRowNum;

            for (int i = 1; i < rowCount; i++)
            {
                ICell cell = sheet.GetRow(i).GetCell(2);
                customerList.Add(cell.ToString());
            }

            customerList.Distinct().ToList();
            Console.WriteLine("Raeding of Customer List is complete ");
            Console.WriteLine("Number of unique customer is  = {0}", customerList.Count);
        }


        if (sheet != null)
        {
            int rowCount = sheet.LastRowNum;

            for (int i = 1; i < rowCount; i++)
            {
                ICell cell = sheet.GetRow(i).GetCell(7);
                pg.Items.Add(cell.ToString());
            }

            Items.Distinct().ToList();
        }
    }
    if (v1 == "Write")
    {
        if (sheet != null)
        {

            int rowCount = sheet.LastRowNum;

            for (int i = 1; i < rowCount; i++)
            {
                IRow row = sheet.GetRow(i);

                // get the first cell
                ICell cell = row.GetCell(2);
              //  ICell custCell = sheet.GetRow(i).GetCell(2);
                var routeid = items1.Where(p => p.GtmuCustId == cell.ToString()).First().RteId;
                var locationName = items1.Where(p => p.GtmuCustId == cell.ToString()).First().LocNm;
                Console.WriteLine(locationName);
                Console.WriteLine(routeid);

                ICell updatecellRoute = row.CreateCell(11);
                ICell updatecellLocation = row.CreateCell(10);
                //throw new NotImplementedException();
             //   row.CreateCell(10).SetCellValue(routeid);
             //   row.CreateCell(11).SetCellValue(locationName);
                updatecellRoute.SetCellValue(routeid);
                updatecellLocation.SetCellValue(locationName);
            }
            wb.Write(fs);
        }
    }
}
0

There are 0 best solutions below