using System; using System.Data; using System.Windows.Forms; using MiniSqlQuery.Core; using MiniSqlQuery.Core.Forms; namespace MiniSqlQuery.Exports.Plugin { public partial class ExportWindow : Form { private readonly IApplicationServices _services; private DataSet _dsExecutedData; public ExportWindow(IApplicationServices services) { _services = services; InitializeComponent(); txtFilePath.Text = string.Format("{0}\\export{1:yyyy-MM-dd}.htm", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateTime.Today); } public string SetStatusText { set { toolStripStatusLabel1.Text = value; statusStrip1.Refresh(); } } private void ExportWindow_Load(object sender, EventArgs e) { IQueryBatchProvider batchProvider = _services.HostWindow.ActiveChildForm as IQueryBatchProvider; if (batchProvider != null && batchProvider.Batch != null) { if (batchProvider.Batch.Queries.Count > 1) { BatchQuerySelectForm querySelectForm = new BatchQuerySelectForm(); querySelectForm.Fill(batchProvider.Batch); querySelectForm.ShowDialog(); if (querySelectForm.DialogResult == DialogResult.OK) { _dsExecutedData = querySelectForm.SelectedQuery.Result; } else { Close(); // user calncelled } return; } if (batchProvider.Batch.Queries.Count == 1) { _dsExecutedData = batchProvider.Batch.Queries[0].Result; return; } } MessageBox.Show("Couldn't find a result window, run a query or view a table to export the data."); Close(); } private void btnExport_Click(object sender, EventArgs e) { if (rbtXml.Checked) { ExportXml(); } if (rbtHtml.Checked) { ExportHtml(); } if (rbtCsv.Checked) { ExportCSV(); } } private void button1_Click(object sender, EventArgs e) { // Create new SaveFileDialog object SaveFileDialog dialogSave = new SaveFileDialog(); // Default file extension if (rbtCsv.Checked) { dialogSave.DefaultExt = "csv"; dialogSave.FilterIndex = 2; } if (rbtHtml.Checked) { dialogSave.DefaultExt = "htm"; dialogSave.FilterIndex = 1; } if (rbtXml.Checked) { dialogSave.DefaultExt = "xml"; dialogSave.FilterIndex = 3; } //DialogSave.DefaultExt = "txt"; // Available file extensions dialogSave.Filter = "Html File (*.htm)|*.htm|CSV File (*.csv)|*.csv|XML file (*.xml)|*.xml"; // Adds a extension if the user does not dialogSave.AddExtension = true; // Restores the selected directory, next time dialogSave.RestoreDirectory = true; // Dialog title dialogSave.Title = "Where do you want to save the file?"; // Startup directory dialogSave.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // Show the dialog and process the result if (dialogSave.ShowDialog() == DialogResult.OK) { txtFilePath.Text = dialogSave.FileName; //MessageBox.Show("You selected the file: " + DialogSave.FileName); } dialogSave.Dispose(); } //private int GetFieldCount //{ // get { return _dsExecutedData.Tables[0].Columns.Count; } //} //private int GetRowCount //{ // get { return _dsExecutedData.Tables[0].Rows.Count; } //} private void ExportHtml() { Export.HtmlExportFormat format = new Export.HtmlExportFormat(); format.FontColor = txtFontColor.Text; format.FontFamily = txtFontFamily.Text; format.FontSize = txtFontSize.Text; format.HeaderColor = txtHeaderBGColor.Text; format.RowAltColor = txtRowBgAltColor.Text; format.RowColor = txtRowBgcolor.Text; Export.HtmlExport.OnWrittenData += CSVExport_OnWrittenData; Export.HtmlExport.ExportToHTML(_dsExecutedData.Tables[0], txtFilePath.Text, format); #region Not used //StringBuilder sbCss = new StringBuilder(); //StringBuilder sbHtml = new StringBuilder(); //bool isAltSet = false; //sbCss.Append(""); //this.SetStatusText = "Created style for html"; //sbHtml.Append(""); //sbHtml.Append("
{0} | ", _dsExecutedData.Tables[0].Columns[i].ColumnName)); // this.SetStatusText = "Writing column name " + i.ToString(); //} //sbHtml.Append(""); //int Counter = 0; //foreach (DataRow dr in _dsExecutedData.Tables[0].Rows) //{ // sbHtml.Append("|
{0} | ", dr[i].ToString())); // } // else // { // sbHtml.Append(string.Format("{0} | ", dr[i].ToString())); // } // } // Counter++; // this.SetStatusText = "Wring row " + Counter.ToString(); // sbHtml.Append("