گرفتن خروجی Excel از داده های کنترل ListView
کد:
void ExportToExcel(ListView lstView)
{
SaveFileDialog DlgSaveExcel = new SaveFileDialog();
DlgSaveExcel.DefaultExt = "csv";
DlgSaveExcel.****** = "csv files (*.csv)|*.csv";
if (DlgSaveExcel.ShowDialog() == DialogResult.OK)
{
Stream myStream;
byte[] info = null;
string ExportExcel = string.Empty;
if ((myStream = DlgSaveExcel.OpenFile()) != null)
{
foreach (ColumnHeader Curr in lstView.Columns)
{
ExportExcel += Curr.Text.Replace(";", "") + ";";
}
ExportExcel += "\n";
foreach (ListViewItem Item in lstView.Items)
{
foreach (ListViewItem.ListViewSubItem SubItem in Item.SubItems)
{
ExportExcel += SubItem.Text.Replace(";", "") + ";";
}
ExportExcel += "\n";
}
info = new UTF8Encoding(true).GetBytes(ExportExcel);
myStream.Write(info, 0, info.Length);
myStream.Close();
}