using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TyroDeveloperDLL
{
public class ListViewToExcel
{
///
/// Export to Excel
///
/// Name of the ListView control
/// Book name
/// Folder to put the file.
/// To export a list of items
/// C#: ExportToExcel(lvArticulos,"Items","C:\");
public static void ExportToExcel(ListView lv, string prmBookName, string prmPath)
{
try
{
if (lv.Items.Count == 0)
{
MessageBox.Show("No items to export",
"System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
string[] st = new string[lv.Columns.Count];
DirectoryInfo di = new DirectoryInfo(prmPath);
if (di.Exists == false) di.Create();
StreamWriter sw = new StreamWriter(prmPath + prmBookName + ".xls", false);
sw.AutoFlush = true;
for (int col = 0; col < lv.Columns.Count; col++)
{
sw.Write("\t" + lv.Columns[col].Text.ToString());
}
int rowIndex = 1; int row = 0;
string st1 = "";
for (row = 0; row < lv.Items.Count; row++)
{
if (rowIndex <= lv.Items.Count)
rowIndex++;
if (row == 0) st1 = "\n";
else st1 = "";
for (int col = 0; col < lv.Columns.Count; col++)
{
st1 = st1 + "\t" + "'" + lv.Items[row].SubItems[col].Text.ToString();
}
sw.WriteLine(st1);
}
sw.Close();
FileInfo fil = new FileInfo(prmPath + prmBookName + ".xls");
if (fil.Exists == true)
MessageBox.Show("¡Finished!\n" +
"File Name: " + prmPath + prmBookName,
"Export to Excel",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Please visit: www.TyroDeveloper.com
Please Click on +1
y las cabeceras??
ReplyDeleteTHANKSSSSS
ReplyDeleteThanks
ReplyDelete