private void TlbBtnExport_Click(object sender, EventArgs e) { SaveFileDialog fileDialog = new SaveFileDialog(); fileDialog.Title = "Select location for exported file"; fileDialog.Filter = "Comma-separated values (*.csv)|*.csv|Html (*.htm)|*.htm"; fileDialog.FilterIndex = 2; fileDialog.RestoreDirectory = true; if (DialogResult.OK != fileDialog.ShowDialog()) { return; } try { Exporter.ExportedData data = GetExportedData(); switch (fileDialog.FilterIndex) { case 1: Exporter.ExportAsCsv(fileDialog.FileName, data); break; case 2: Exporter.ExportAsHtml(fileDialog.FileName, data); break; } } catch (System.Exception a_Exception) { ErrorMessageBox.Show("Failed to export data:\n" + a_Exception.Message); } }
private Exporter.ExportedData GetExportedData() { List <int> columnOrder = GetVisibleColumnOrder(); int numHeaderItems = 1; Exporter.ExportedData result = new Exporter.ExportedData(); result.Aligns = new HorizontalAlignment[columnOrder.Count]; result.Rows = new Exporter.ExportedRow[m_ItemList.Length + numHeaderItems]; // Aligns for (int j = 0; j < columnOrder.Count; j++) { int actualColumn = columnOrder[j]; result.Aligns[j] = LstRefinery.Columns[actualColumn].TextAlign; } // Header Exporter.ExportedRow currRow = new Exporter.ExportedRow(); result.Rows[0] = currRow; currRow.Color = Color.LightGray; currRow.Cells = new String[columnOrder.Count]; for (int j = 0; j < currRow.Cells.Length; j++) { int actualColumn = columnOrder[j]; currRow.Cells[j] = LstRefinery.Columns[actualColumn].Text; } // Visible items for (int i = 0; i < m_ItemList.Length; i++) { RetrieveVirtualItemEventArgs itemData = new RetrieveVirtualItemEventArgs(i); Refinery_RetrieveVirtualItem(this, itemData); currRow = new Exporter.ExportedRow(); result.Rows[i + numHeaderItems] = currRow; currRow.Color = itemData.Item.BackColor; currRow.Cells = new String[columnOrder.Count]; for (int j = 0; j < currRow.Cells.Length; j++) { int actualColumn = columnOrder[j]; currRow.Cells[j] = itemData.Item.SubItems[actualColumn].Text; } } return(result); }