How to properly export excel data to a listview using epplus?

don bradman 621 Reputation points
2021-12-27T03:53:58.79+00:00

I'm trying to export data from a .xlsx file to a Listview control where the date of the "Due Date" column falls between today's date and the next 10 days from today and below is my code (I'm using EPPlus library for excel file stuff):

using (ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(filename), false))  
 {  
 ExcelWorksheet mainSheet = package.Workbook.Worksheets.First();  
  
 listView1.Columns.Add("Party");  
 listView1.Columns.Add("Bill No.");  
 listView1.Columns.Add("Bill Date");  
 listView1.Columns.Add("Amount");  
 listView1.Columns.Add("Due Date");  
  
  
 for (int row = 2; row <= mainSheet.Dimension.End.Row; row++)  
  
 {   
  
 for (int col = 1; col < 6; col++)  
 {   
  
  
 if (DateTime.Parse(mainSheet.Cells[row, 5].Text) >= DateTime.Today && DateTime.Parse(mainSheet.Cells[row, 5].Text) <= DateTime.Today.AddDays(10))  
 {  
 string[] arr = new string[5];  
 ListViewItem itm ;  
 arr[col-1] = mainSheet.Cells[row, col].Text;  
  
 itm = new ListViewItem(arr);  
  
 listView1.Items.Add(itm);  
  
 }  
 }  
 }  
  
 }  

160466-untitled.png

As you can see the data is not showing properly in Listview, how do I fix this?

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-12-27T06:19:50.373+00:00

    @don bradman , you could try the following code to export excel data to a listview using epplus properly.

    Code:

      private void Form1_Load(object sender, EventArgs e)  
        {  
            string filename = "D:\\test1.xlsx";  
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;  
            using (ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(filename), false))  
            {  
             
                ExcelWorksheet mainSheet = package.Workbook.Worksheets.First();  
      
                listView1.Columns.Add("Party");  
                listView1.Columns.Add("Bill No.");  
                listView1.Columns.Add("Bill Date");  
                listView1.Columns.Add("Amount");  
                listView1.Columns.Add("Due Date");  
                listView1.View = View.Details;  
      
                for (int row = 2; row <= mainSheet.Dimension.End.Row; row++)  
                {  
                    ListViewItem itm = new ListViewItem();  
                    for (int col = 2; col < 6; col++)  
                    {  
                        if(DateTime.Parse(mainSheet.Cells[row, 5].Text) >= DateTime.Today && DateTime.Parse(mainSheet.Cells[row, 5].Text) <= DateTime.Today.AddDays(10))  
                        {  
                            itm.Text =mainSheet.Cells[row, 1].Text;  
      
                            itm.SubItems.Add(mainSheet.Cells[row, col].Text);  
                             
                        }  
                         
                    }  
                     if(itm.SubItems.Count>1)  
                    {  
                        listView1.Items.Add(itm);  
                    }  
      
      
      
                }  
      
            }  
        }  
    

    Result:

    160539-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.