miniSql

创建
zgc123@gmail.com authored at 11/19/2023 1:40:15 AM
6136600
Tree
0 Parent(s)
Summary: 3 changed files with 200 additions and 0 deletions.
Added +98 -0
Added +89 -0
Added +13 -0
Added +98 -0
diff --git a/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/CSVExport.cs b/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/CSVExport.cs
new file mode 100644
index 0000000..b55807a
--- /dev/null
+++ b/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/CSVExport.cs
@@ -0,0 +1,98 @@
+using System;
+using System.Data;
+using System.IO;
+
+namespace MiniSqlQuery.Exports.Plugin.Export
+{
+    public class CSVExport
+    {
+        #region Delegates
+
+        public delegate void WrittenData(string text);
+
+        #endregion
+
+        public static event WrittenData OnWrittenData;
+
+        public static void ExportToCSV(DataTable source, string fileName, bool fileNamesFirstRow)
+        {
+            // Create the CSV file to which grid data will be exported.
+            StreamWriter sw = new StreamWriter(fileName, false);
+            // First we will write the headers.
+            DataTable dt = source;
+            int iColCount = dt.Columns.Count;
+
+            if (fileNamesFirstRow)
+            {
+                for (int i = 0; i < iColCount; i++)
+                {
+                    CsvWrite(sw, dt.Columns[i].ColumnName);
+                    if (i < iColCount - 1)
+                    {
+                        sw.Write(",");
+                    }
+                    if (OnWrittenData != null)
+                    {
+                        OnWrittenData(string.Format("Wrote column name {0}", i));
+                    }
+                }
+                sw.Write(sw.NewLine);
+                if (OnWrittenData != null)
+                {
+                    OnWrittenData("Wrote filednames row..");
+                }
+            }
+            // Now write all the rows.
+            int counter = 0;
+            foreach (DataRow dr in dt.Rows)
+            {
+                for (int i = 0; i < iColCount; i++)
+                {
+                    if (!Convert.IsDBNull(dr[i]))
+                    {
+                        CsvWrite(sw, dr[i].ToString());
+                    }
+                    if (i < iColCount - 1)
+                    {
+                        sw.Write(",");
+                    }
+                }
+                sw.Write(sw.NewLine);
+                counter++;
+                if (OnWrittenData != null)
+                {
+                    OnWrittenData(string.Format("Wrote row {0}", counter));
+                }
+            }
+            sw.Close();
+            if (OnWrittenData != null)
+            {
+                OnWrittenData("Finished exporting CSV file to " + fileName);
+            }
+        }
+
+        /// <summary>
+        /// Perform a CSV compliant wtrite of <paramref name="text"/> to the <paramref name="sw"/>.
+        /// Handles commas, quotes and newlines.
+        /// </summary>
+        /// <param name="sw">The writer.</param>
+        /// <param name="text">The text.</param>
+        private static void CsvWrite(TextWriter sw, string text)
+        {
+            if (text != null)
+            {
+                bool needsQuotes = false;
+                if (text.Contains("\"") || text.Contains(",") || text.Contains("\n"))
+                {
+                    needsQuotes = true;
+                    text = text.Replace("\"", "\"\"");
+                }
+                if (needsQuotes)
+                {
+                    text = string.Concat("\"", text, "\"");
+                }
+                sw.Write(text);
+            }
+        }
+    }
+}
\ No newline at end of file
Added +89 -0
diff --git a/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/HtmlExport.cs b/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/HtmlExport.cs
new file mode 100644
index 0000000..3a989af
--- /dev/null
+++ b/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/HtmlExport.cs
@@ -0,0 +1,89 @@
+using System.Data;
+using System.Text;
+
+namespace MiniSqlQuery.Exports.Plugin.Export
+{
+    public class HtmlExport
+    {
+        public delegate void WrittenData(string text);
+
+        public static event WrittenData OnWrittenData;
+
+        public static void ExportToHTML(DataTable source, string fileName, HtmlExportFormat format)
+        {
+            StringBuilder sbCss = new StringBuilder();
+            StringBuilder sbHtml = new StringBuilder();
+            bool isAltSet = false;
+
+            sbCss.Append("<style>");
+            sbCss.Append("body { font-family:" + format.FontFamily + "; font-size:" + format.FontSize + "; color:" + format.FontColor + "; }");
+            sbCss.Append(".Header {background-color:" + format.HeaderColor + "}");
+            sbCss.Append(".Row    {background-color:" + format.RowColor + "}");
+            sbCss.Append(".AltRow    {background-color:" + format.RowAltColor + "}");
+            sbCss.Append("</style>");
+
+            //this.SetStatusText = "Created style for html";
+
+            sbHtml.Append("<html>");
+            sbHtml.Append("<head><title>Export from " + source.TableName + "</title>");
+            sbHtml.Append(sbCss.ToString());
+            sbHtml.Append("</head>");
+            sbHtml.Append("<body>");
+
+            int fields = source.Columns.Count;
+            sbHtml.Append("<table border='0' cellpadding='2'");
+            sbHtml.Append("<tr>");
+            for (int i = 0; i < fields; i++)
+            {
+                sbHtml.Append(string.Format("<td class='Header'>{0}</td>", source.Columns[i].ColumnName));
+
+                if (OnWrittenData != null)
+                {
+                    OnWrittenData("Writing column name " + i);
+                }
+            }
+            sbHtml.Append("</tr>");
+
+            int counter = 0;
+            foreach (DataRow dr in source.Rows)
+            {
+                sbHtml.Append("<tr>");
+
+                for (int i = 0; i < fields; i++)
+                {
+                    if (isAltSet)
+                    {
+                        sbHtml.Append(string.Format("<td class='AltRow'>{0}</td>", dr[i]));
+                    }
+                    else
+                    {
+                        sbHtml.Append(string.Format("<td class='Row'>{0}</td>", dr[i]));
+                    }
+                }
+                counter++;
+                if (OnWrittenData != null)
+                {
+                    OnWrittenData("Writing row " + counter);
+                }
+
+                sbHtml.Append("</tr>");
+
+                if (isAltSet == false)
+                    isAltSet = true;
+                else
+                    isAltSet = false;
+            }
+            sbHtml.Append("</table>");
+            sbHtml.Append("</body></html>");
+
+
+            System.IO.TextWriter tw = new System.IO.StreamWriter(fileName);
+            tw.WriteLine(sbHtml.ToString());
+            tw.Close();
+            if (OnWrittenData != null)
+            {
+                OnWrittenData("Finished exporting to html file");
+            }
+        }
+    }
+}
\ No newline at end of file
Added +13 -0
diff --git a/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/HtmlExportFormat.cs b/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/HtmlExportFormat.cs
new file mode 100644
index 0000000..fdd0c14
--- /dev/null
+++ b/minisqlquery-master/src/Contrib/MiniSqlQuery.Exports.Plugin/Export/HtmlExportFormat.cs
@@ -0,0 +1,13 @@
+namespace MiniSqlQuery.Exports.Plugin.Export
+{
+    public class HtmlExportFormat
+    {
+        public string FontFamily { get; set; }
+        public string FontSize { get; set; }
+        public string FontColor { get; set; }
+
+        public string HeaderColor { get; set; }
+        public string RowColor { get; set; }
+        public string RowAltColor { get; set; }
+    }
+}
\ No newline at end of file