/// <summary> /// Exports a listview to exvcell /// </summary> /// <param name="lvi">The ListView to export to Excel</param> /// <param name="saveName">The name that the excel file will be saved as</param> /// <returns>If the grid was successfully exported and file saved</returns> public static bool ExportToExcell(ListView lvi, string saveName) { lvToExport = lvi; string strExtension = ".xls"; string fullname = ""; if (lvToExport != null) { try { saveName = checkFileExists(saveName, strExtension, pathExports); fullname = pathExports + saveName + strExtension; StreamWriter sw = new StreamWriter(fullname, false); sw.AutoFlush = true; for (int col = 0; col < lvToExport.Columns.Count; col++) { sw.Write(lvToExport.Columns[col].Text.ToString() + "\t"); } int rowIndex = 1; int row = 0; string st1 = ""; for (row = 0; row < lvToExport.Items.Count; row++) { if (rowIndex <= lvToExport.Items.Count) rowIndex++; st1 = "\n"; for (int col = 0; col < lvToExport.Columns.Count; col++) { if (lvToExport.Items[row].SubItems[col].Text.ToString() == "") { st1 += "NULL" + lvToExport.Items[row].SubItems[col].Text.ToString(); } else { st1 += lvToExport.Items[row].SubItems[col].Text.ToString(); } st1 = st1 + "\t"; } sw.Write(st1); //sw.Flush(); } sw.Close(); FileInfo fil = new FileInfo(fullname); if (fil.Exists == true) { if (MessageBox.Show("Grid has been exported to file" + Environment.NewLine + fil.FullName + Environment.NewLine + "Do you want to open file now?", "Export To Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { CCFBGlobal.openDocumentOutsideCCFB(fil.FullName); } return true; } } catch (Exception ex) { FileInfo file = new FileInfo(pathExports + saveName + strExtension); appendErrorToErrorReport(lvToExport.ToString(), ex.GetBaseException().ToString()); MessageBox.Show("ERROR: Could not export the grid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } return true; }