Ejemplo n.º 1
0
        private void ToXlsFile(ListView lv_items, string strFileName, string strPageName)
        {
            Cursor.Current = Cursors.WaitCursor;
            DataTable tab = new DataTable(strPageName);

            for (int i = 1; i <= lv_items.Columns.Count; i++)
            {
                System.Type tag = lv_items.Columns[i - 1].Tag as System.Type;
                tab.Columns.Add(lv_items.Columns[i - 1].Text, tag);
            }
            foreach (ListViewItem item in lv_items.Items)
            {
                DataRow row = tab.NewRow();
                for (int j = 1; j <= lv_items.Columns.Count; j++)
                {
                    System.Type type2 = lv_items.Columns[j - 1].Tag as System.Type;
                    if (j == 1)
                    {
                        if (string.IsNullOrEmpty(item.Text))
                        {
                            row[lv_items.Columns[j - 1].Text] = DBNull.Value;
                        }
                        else if (type2 == typeof(decimal))
                        {
                            row[lv_items.Columns[j - 1].Text] = Convert.ToDecimal(item.Text);
                        }
                        else
                        {
                            row[lv_items.Columns[j - 1].Text] = item.Text;
                        }
                    }
                    else if (string.IsNullOrEmpty(item.SubItems[j - 1].Text))
                    {
                        row[lv_items.Columns[j - 1].Text] = DBNull.Value;
                    }
                    else if (type2 == typeof(decimal))
                    {
                        row[lv_items.Columns[j - 1].Text] = Convert.ToDecimal(item.SubItems[j - 1].Text);
                    }
                    else
                    {
                        row[lv_items.Columns[j - 1].Text] = item.SubItems[j - 1].Text;
                    }
                }
                tab.Rows.Add(row);
            }
            ExcelProvider.SetExcelData(strFileName, tab);
            Cursor.Current = Cursors.Default;
        }