【基於WinForm+Access局域網共享數據庫的項目總結】之篇二:WinForm開發扇形圖統計和Excel數據導出


篇一:WinForm開發總體概述與技術實現

篇二:WinForm開發扇形圖統計和Excel數據導出

篇三:Access遠程連接數據庫和窗體打包部署


【小記】:最近基於WinForm+Access數據庫完成一個法律咨詢管理系統。本系統要求類似網頁后台管理效果,並且基於局域網內,完成多客戶端操作同一數據庫,根據權限不同分別執行不同功能模塊。核心模塊為級聯統計類型管理、數據庫咨詢數據扇形統計、樹的操作、咨詢數據的管理、手寫分頁、Excel數據的導出、多用戶操作服務器數據等。並支持多用戶同時操作,遠程連接數據庫且對數據IP信息的修改。開發過程中特別對界面的要求和事后多用戶操作顯得略為麻煩。自此,本項目得以完善交付,然后對其進行小結。依舊采用整個框架認識,核心知識逐個梳理分析,以便於二次開發和需要之程序員共享。

 


篇二:WinForm開發扇形圖統計和Excel數據導出

【開篇】本章概述


 

開發過程簡介:應客戶需求,在法律咨詢系統中以扇形圖形式進行數據統計。需求如下:(圖表需要統計的數據 各咨詢類型小類的數量、以及占大類的百分比,可隨意組合生成。例如,(以民事 - 婚姻家庭 - 撫養為例)所有民事的占總案件的比例,婚姻家庭占所有民事的比例,撫養占所有婚姻家庭的比例。)現在核心問題在於兩點,第一、如果單級易於把握,但是多級無限組合似乎有難度;第二、web和wpf等開發都有現成的控件或者組件應用經驗,而winfrom的相關控件不熟悉;第三、只能通過網上查詢相關資料,具體查詢結果我做個總結,以供需要之人,免得浪費時間。

一、扇形統計圖解決思路:(以下資料中包含扇形圖、餅狀圖、折線圖)

1 下個MSChart控件,里面可以直接用
2 C#編寫的圖像:http://www.cnblogs.com/ziyiFly/archive/2008/09/24/1297841.html
3 比較簡潔:http://netdz.blog.163.com/blog/static/2106990252012725104054804/
4 參考:http://jingyan.baidu.com/article/e75aca858b6630142edac6de.html
但是,最終以上思路還沒有解決問題。因為我采用的直接C#代碼繪制圖片顯示方式,然后圖片pictureBox控件中顯示。這樣會出現至上而下流形式的輸出。使得頁面布局效果很差。最終自己改進后,采用兩個pictureBox控件,一個用於輸出圖片流,另一個用於輸出文字流。實現效果如下(三級實現,下面顯示2級的效果,因為后台核心代碼基本一致)

 

 

程序編寫思路:

1、窗體頁面放置兩個pictureBox控件顯示圖片和數據信息.

2、構造Cartogram統計圖類,然后構造方法,最后講控件名和查詢數據文本傳參。Cartogram.CreateTestImage(pictureBox1, pictureBox2, comboBox1.Text);

3、在統計方法中首先模糊查詢獲取總的條目,然后將類型表中所有那列類型數據保存在數組中,以便下面遍歷。

       public static ArrayList BindB(string type)
       {
           ArrayList al = new ArrayList();
           string sql = "select * from B_type where aname='" + type + "'";
           DataSet ds = MYHelper.SQLHelper.GetSetData(sql);
           if (ds.Tables[0].Rows.Count > 0)
           {
               for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
               {
                   al.Add(ds.Tables[0].Rows[j]["bname"].ToString());
               }
           }

           return al;
       }

4、再進行遍歷數組,查到每個類型的條目,然后比總數獲取百分比進行在圓圖上比例顯示。到此每個類型所占比例的統計圖構造完成,那么怎么打印數目和百分比?

           int childtype = 0;
           ArrayList coun = new ArrayList();
           for (int i = 0; i < a.Count; i++)
           {
               //子事件總數
               childtype = MYHelper.SQLHelper.ExecuteScalar("Select count(*) From L_consult Where type Like '" + "%" + a[i].ToString() + "%" + "'");
               //畫占扇形圖的比例
               objgraphics.FillPie((SolidBrush)colors[i], pierect, currentdegree, Convert.ToSingle(childtype) / Total * 360);
               currentdegree += Convert.ToSingle(childtype) / Total * 360;
               coun.Add(childtype);
           }

5、依舊采用遍歷方式,將上面遍歷的每個類型條目輸出,然后除總數目轉化成百分比。最后實例化兩個圖片對象進行輸出

           for (int i = 0; i < a.Count; i++)
           {
               kuandu += 20;
               y += 20;
               objgraphics1.DrawString(a[i] + "事件: " + (coun[i]), fontlegend, blackbrush, x, kuandu);
               objgraphics1.FillRectangle((SolidBrush)colors[i], 20, y, 10, 10);
               objgraphics1.DrawString("所占比率: " + Convert.ToString((Convert.ToSingle(coun[i]) / Total) * 100) + " %", fontlegend, blackbrush, 200, kuandu);
           }

6、網頁輸出和窗體輸出的區別?即本代碼適應於網頁和窗體

網頁輸出圖片流:

Response.ContentType = "image/Jpeg";
objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
objgraphics.Dispose();
objbitmap.Dispose();

窗體輸出圖片流:

           pictureBox1.Image = objbitmap;
           pictureBox2.Image = objbitmap1;

完整統計圖源碼:

       static string date = DateTime.Now.ToString("yyyy-MM-dd");

       public static ArrayList BindA()
       {
           ArrayList al = new ArrayList();
           string sql = "select * from A_type";
           DataSet ds = MYHelper.SQLHelper.GetSetData(sql);
           if (ds.Tables[0].Rows.Count > 0)
           {
               for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
               {
                   al.Add(ds.Tables[0].Rows[j]["name"].ToString());
               }
           }
           return al;
       }
       public static ArrayList BindB(string type)
       {
           ArrayList al = new ArrayList();
           string sql = "select * from B_type where aname='" + type + "'";
           DataSet ds = MYHelper.SQLHelper.GetSetData(sql);
           if (ds.Tables[0].Rows.Count > 0)
           {
               for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
               {
                   al.Add(ds.Tables[0].Rows[j]["bname"].ToString());
               }
           }

           return al;
       }
       public static ArrayList BindC(string type)
       {
           ArrayList al = new ArrayList();
           string sql = "select * from C_type where bname='" + type + "'";
           DataSet ds = MYHelper.SQLHelper.GetSetData(sql);
           if (ds.Tables[0].Rows.Count > 0)
           {
               for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
               {
                   al.Add(ds.Tables[0].Rows[j]["cname"].ToString());
               }
           }

           return al;
       }
       //一級統計圖
       public static void CreateTestImage(PictureBox pictureBox1, PictureBox pictureBox2)
       {
           ArrayList a = BindA();
           //把連接字串指定為一個常量
           float Total = MYHelper.SQLHelper.ExecuteScalar("select count(*) from L_consult");//獲取總的條目數
           #region
           //設置字體,fonttitle為主標題的字體
           Font fontlegend = new Font("verdana", 9);
           Font fonttitle = new Font("verdana", 10, FontStyle.Bold);

           //背景寬
           int width = 350;
           int bufferspace = 15;
           int legendheight = fontlegend.Height * 12 + bufferspace; //高度
           int titleheight = fonttitle.Height + bufferspace;
           int height = width + legendheight + titleheight + bufferspace;//白色背景高
           int pieheight = width;
           Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);

           //加上各種隨機色
           ArrayList colors = new ArrayList();
           Random rnd = new Random();
           for (int i = 0; i < 50; i++)
               colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));

           //創建一個bitmap實例
           Bitmap objbitmap = new Bitmap(width, height);
           Graphics objgraphics = Graphics.FromImage(objbitmap);

           Bitmap objbitmap1 = new Bitmap(width, height);
           Graphics objgraphics1 = Graphics.FromImage(objbitmap1);

           //畫一個白色背景
           objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);

           //畫一個亮黃色背景 
           objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);

           //以下為畫餅圖(有幾行row畫幾個)
           float currentdegree = 0.0f;
           #endregion
           int childtype = 0;
           ArrayList coun = new ArrayList();
           for (int i = 0; i < a.Count; i++)
           {
               //子事件總數
               childtype = MYHelper.SQLHelper.ExecuteScalar("Select count(*) From L_consult Where type Like '" + "%" + a[i].ToString() + "%" + "'");
               //畫子事件總數
               objgraphics.FillPie((SolidBrush)colors[i], pierect, currentdegree, Convert.ToSingle(childtype) / Total * 360);
               currentdegree += Convert.ToSingle(childtype) / Total * 360;
               coun.Add(childtype);
           }

           //以下為生成主標題
           SolidBrush blackbrush = new SolidBrush(Color.Black);
           SolidBrush bluebrush = new SolidBrush(Color.Blue);
           string title = " 律師事務所統計餅狀圖: " + "\n \n\n";
           StringFormat stringFormat = new StringFormat();
           stringFormat.Alignment = StringAlignment.Center;
           stringFormat.LineAlignment = StringAlignment.Center;
           objgraphics.DrawString(title, fonttitle, blackbrush, new Rectangle(0, 10, width, titleheight), stringFormat);

           //列出各字段與得數目
           objgraphics1.DrawRectangle(new Pen(Color.White, 2), 0, 0, 400, 400);

           objgraphics1.DrawString("---------------咨詢事件統計信息---------------------", fontlegend, bluebrush, 0, 10);
           objgraphics1.DrawString("統計單位: " + "律師事務所", fontlegend, blackbrush, 40, 40);
           objgraphics1.DrawString("統計年份: " + date, fontlegend, blackbrush, 40, 70);
           objgraphics1.DrawString("事件咨詢總數: " + Convert.ToString(Total), fontlegend, blackbrush, 40, 100);
           int kuandu = 100;
           int y = 100;
           int x = 40;
           for (int i = 0; i < a.Count; i++)
           {
               kuandu += 30;
               y += 30;
               objgraphics1.DrawString(a[i] + "事件: " + (coun[i]), fontlegend, blackbrush, x, kuandu);
               objgraphics1.FillRectangle((SolidBrush)colors[i], 20, y, 10, 10);
               objgraphics1.DrawString("所占比率: " + Convert.ToString((Convert.ToSingle(coun[i]) / Total) * 100) + " %", fontlegend, blackbrush, 160, kuandu);
           }
           pictureBox1.Image = objbitmap;
           pictureBox2.Image = objbitmap1;
       }

       //二級統計圖
       public static void CreateTestImage(PictureBox pictureBox1, PictureBox pictureBox2, string type)
       {
           ArrayList a = BindB(type);
           //把連接字串指定為一個常量
           float Total = MYHelper.SQLHelper.ExecuteScalar("Select count(*) From L_consult Where type Like '" + "%" + type + "%" + "'");//獲取總的條目數
           #region
           //設置字體,fonttitle為主標題的字體
           Font fontlegend = new Font("verdana", 9);
           Font fonttitle = new Font("verdana", 10, FontStyle.Bold);

           //背景寬
           int width = 350;
           int bufferspace = 15;
           int legendheight = fontlegend.Height * 12 + bufferspace; //高度
           int titleheight = fonttitle.Height + bufferspace;
           int height = width + legendheight + titleheight + bufferspace;//白色背景高
           int pieheight = width;
           Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);

           //加上各種隨機色
           ArrayList colors = new ArrayList();
           Random rnd = new Random();
           for (int i = 0; i < 50; i++)
               colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));

           //創建一個bitmap實例
           Bitmap objbitmap = new Bitmap(width, height);
           Graphics objgraphics = Graphics.FromImage(objbitmap);

           Bitmap objbitmap1 = new Bitmap(width, height);
           Graphics objgraphics1 = Graphics.FromImage(objbitmap1);

           //畫一個白色背景
           objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);

           //畫一個亮黃色背景 
           objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);

           //以下為畫餅圖(有幾行row畫幾個)
           float currentdegree = 0.0f;
           #endregion
           int childtype = 0;
           ArrayList coun = new ArrayList();
           for (int i = 0; i < a.Count; i++)
           {
               //子事件總數
               childtype = MYHelper.SQLHelper.ExecuteScalar("Select count(*) From L_consult Where type Like '" + "%" + a[i].ToString() + "%" + "'");
               //畫占扇形圖的比例
               objgraphics.FillPie((SolidBrush)colors[i], pierect, currentdegree, Convert.ToSingle(childtype) / Total * 360);
               currentdegree += Convert.ToSingle(childtype) / Total * 360;
               coun.Add(childtype);
           }

           //以下為生成主標題
           SolidBrush blackbrush = new SolidBrush(Color.Black);
           SolidBrush bluebrush = new SolidBrush(Color.Blue);
           string title = " 律師事務所統計餅狀圖: " + "\n \n\n";
           StringFormat stringFormat = new StringFormat();
           stringFormat.Alignment = StringAlignment.Center;
           stringFormat.LineAlignment = StringAlignment.Center;
           objgraphics.DrawString(title, fonttitle, blackbrush, new Rectangle(0, 10, width, titleheight), stringFormat);

           //列出各字段與得數目
           objgraphics1.DrawRectangle(new Pen(Color.White, 2), 0, 0, 500, 500);

           objgraphics1.DrawString("---------------咨詢事件統計信息---------------------", fontlegend, bluebrush, 0, 10);
           objgraphics1.DrawString("統計單位: " + "律師事務所", fontlegend, blackbrush, 40, 40);
           // objgraphics1.DrawString("【統計時間:】" + date, fontlegend, blackbrush, 40, 40);
           objgraphics1.DrawString("事件咨詢總數: " + Convert.ToString(Total), fontlegend, blackbrush, 40, 60);
           int kuandu = 60;
           int y = 60;
           int x = 40;
           for (int i = 0; i < a.Count; i++)
           {
               kuandu += 20;
               y += 20;
               objgraphics1.DrawString(a[i] + "事件: " + (coun[i]), fontlegend, blackbrush, x, kuandu);
               objgraphics1.FillRectangle((SolidBrush)colors[i], 20, y, 10, 10);
               objgraphics1.DrawString("所占比率: " + Convert.ToString((Convert.ToSingle(coun[i]) / Total) * 100) + " %", fontlegend, blackbrush, 200, kuandu);
           }
           pictureBox1.Image = objbitmap;
           pictureBox2.Image = objbitmap1;
       }

       //三級統計圖
       public static void CreateThreeImage(PictureBox pictureBox1, PictureBox pictureBox2, string bname)
       {
           ArrayList a = BindC(bname);
           //把連接字串指定為一個常量
           float Total = MYHelper.SQLHelper.ExecuteScalar("Select count(*) From L_consult Where type Like '" + "%" + bname + "%" + "'");//獲取總的條目數
           #region
           //設置字體,fonttitle為主標題的字體
           Font fontlegend = new Font("verdana", 9);
           Font fonttitle = new Font("verdana", 10, FontStyle.Bold);

           //背景寬
           int width = 350;
           int bufferspace = 15;
           int legendheight = fontlegend.Height * 12 + bufferspace; //高度
           int titleheight = fonttitle.Height + bufferspace;
           int height = width + legendheight + titleheight + bufferspace;//白色背景高
           int pieheight = width;
           Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);

           //加上各種隨機色
           ArrayList colors = new ArrayList();
           Random rnd = new Random();
           for (int i = 0; i < 80; i++)
               colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));

           //創建一個bitmap實例
           Bitmap objbitmap = new Bitmap(width, height);
           Graphics objgraphics = Graphics.FromImage(objbitmap);

           Bitmap objbitmap1 = new Bitmap(width, height);
           Graphics objgraphics1 = Graphics.FromImage(objbitmap1);

           //畫一個白色背景
           objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);

           //畫一個亮黃色背景 
           objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);

           //以下為畫餅圖(有幾行row畫幾個)
           float currentdegree = 0.0f;
           #endregion
           int childtype = 0;
           ArrayList coun = new ArrayList();
           for (int i = 0; i < a.Count; i++)
           {
               //子事件總數
               childtype = MYHelper.SQLHelper.ExecuteScalar("Select count(*) From L_consult Where type Like '" + "%" + a[i].ToString() + "%" + "'");
               //畫通過人數
               objgraphics.FillPie((SolidBrush)colors[i], pierect, currentdegree, Convert.ToSingle(childtype) / Total * 360);
               currentdegree += Convert.ToSingle(childtype) / Total * 360;
               coun.Add(childtype);
           }

           //以下為生成主標題
           SolidBrush blackbrush = new SolidBrush(Color.Black);
           SolidBrush bluebrush = new SolidBrush(Color.Blue);
           string title = " 律師事務所統計餅狀圖: " + "\n \n\n";
           StringFormat stringFormat = new StringFormat();
           stringFormat.Alignment = StringAlignment.Center;
           stringFormat.LineAlignment = StringAlignment.Center;
           objgraphics.DrawString(title, fonttitle, blackbrush, new Rectangle(0, 10, width, titleheight), stringFormat);

           //列出各字段與得數目
           objgraphics1.DrawRectangle(new Pen(Color.White, 2), 0, 0, 400, 500);

           objgraphics1.DrawString("---------------咨詢事件統計信息---------------------", fontlegend, bluebrush, 0, 10);
           objgraphics1.DrawString("統計單位: " + "律師事務所", fontlegend, blackbrush, 40, 40);
           // objgraphics1.DrawString("【統計時間:】" + date, fontlegend, blackbrush, 40, 40);
           objgraphics1.DrawString("事件咨詢總數: " + Convert.ToString(Total), fontlegend, blackbrush, 40, 60);
           int kuandu = 60;
           int y = 60;
           int x = 40;
           for (int i = 0; i < a.Count; i++)
           {
               kuandu += 20;
               y += 20;
               objgraphics1.DrawString(a[i] + "事件: " + (coun[i]), fontlegend, blackbrush, x, kuandu);
               objgraphics1.FillRectangle((SolidBrush)colors[i], 20, y, 10, 10);
               objgraphics1.DrawString("所占比率: " + Convert.ToString((Convert.ToSingle(coun[i]) / Total) * 100) + " %", fontlegend, blackbrush, 220, kuandu);
           }
           pictureBox1.Image = objbitmap;
           pictureBox2.Image = objbitmap1;
       }
View Code

 

二、對當前頁數據和全部數據導出的功能實現

  首先咱們還是看看效果,然后我說下思路,最后查看源碼:

注釋:當用戶點擊導出當前頁,則以Excel形式導出當前頁信息,當用戶關閉時候,提示是否保存。這樣既可以達到查詢效果,也可以根據需求進行保存。當然導出所有頁效果一樣。

思路:在方法中傳入兩個參數(數據源控件名DataGridView,布爾值flag),在方法中對DataGridView進行遍歷打印出標頭,然后在對各列數據進行填充。最后通過控件器flag控制,如果flag==true,則進行數據保存關閉Excel,如果false直接關閉

補充:有時候,在導出Excel時候,不希望直接顯示關閉時保存,即如上方法。希望直接彈窗,提示保存物理路徑進行保存。在如下代碼,將兩種形式實現方法都進行粘貼出來。

方法一、關閉時保存

    #region DataGridView數據顯示到Excel
        /// <summary>    
        /// 打開Excel並將DataGridView控件中數據導出到Excel   
        /// </summary>    
        /// <param name="dgv">DataGridView對象 </param>    
        /// <param name="isShowExcle">是否顯示Excel界面 </param>    
        /// <remarks>   
        /// add com "Microsoft Excel 11.0 Object Library"   
        /// using Excel=Microsoft.Office.Interop.Excel;   
        /// </remarks>   
        /// <returns> </returns>    
        public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle)
        {
            try
            {
                if (dgv.Rows.Count == 0)
                    return false;
                //建立Excel對象    
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                excel.Application.Workbooks.Add(true);
                excel.Visible = isShowExcle;
                //生成字段名稱    
                for (int i = 0; i < dgv.ColumnCount; i++)
                {
                    excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
                }
                //填充數據    
                for (int i = 0; i < dgv.RowCount; i++)
                {
                    for (int j = 0; j < dgv.ColumnCount; j++)
                    {
                        if (dgv[j, i].ValueType == typeof(string))
                        {
                            excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();
                        }
                        else
                        {
                            excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
                        }
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
View Code

 

方法二、直接提示保存

 #region DateGridView導出到csv格式的Excel
        /// <summary>   
        /// 常用方法,列之間加\t,一行一行輸出,此文件其實是csv文件,不過默認可以當成Excel打開。   
        /// </summary>   
        /// <remarks>   
        /// using System.IO;   
        /// </remarks>   
        /// <param name="dgv"></param>   
        public void DataGridViewToExcel(DataGridView dgv)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存為Excel文件";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = "";
                try
                {
                    //寫入列標題   
                    for (int i = 0; i < dgv.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            columnTitle += "\t";
                        }
                        columnTitle += dgv.Columns[i].HeaderText;
                    }
                    sw.WriteLine(columnTitle);

                    //寫入列內容   
                    for (int j = 0; j < dgv.Rows.Count; j++)
                    {
                        string columnValue = "";
                        for (int k = 0; k < dgv.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                columnValue += "\t";
                            }
                            if (dgv.Rows[j].Cells[k].Value == null)
                                columnValue += "";
                            else
                                columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }
        #endregion
View Code

 

【篇中】技術梳理


 

 

1、如何點擊按鈕顯示彈窗保存或者加載文件?

           SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存為Excel文件";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                 。。。。。
            }
View Code

2、如何遍歷數據控件的列名並寫入Excel?

  for (int i = 0; i < dgv.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            columnTitle += "\t";
                        }
                        columnTitle += dgv.Columns[i].HeaderText;
                    }
                    sw.WriteLine(columnTitle);
View Code

3、如何遍歷數據逐行寫入Excel?

                    //寫入列內容   
                    for (int j = 0; j < dgv.Rows.Count; j++)
                    {
                        string columnValue = "";
                        for (int k = 0; k < dgv.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                columnValue += "\t";
                            }
                            if (dgv.Rows[j].Cells[k].Value == null)
                                columnValue += "";
                            else
                                columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
View Code

4、怎么使用集合保存數據?

       public static ArrayList BindA()
       {
           ArrayList al = new ArrayList();
           string sql = "select * from A_type";
           DataSet ds = MYHelper.SQLHelper.GetSetData(sql);
           if (ds.Tables[0].Rows.Count > 0)
           {
               for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
               {
                   al.Add(ds.Tables[0].Rows[j]["name"].ToString());
               }
           }
           return al;
       }
View Code

5、怎樣顯示隨機顏色?

 Random rnd = new Random();
           for (int i = 0; i < 50; i++)
               colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
View Code

 

【篇末】項目小結

 


到此為止,本篇介紹了窗體顯示扇形圖的思想和代碼。並且提供了支持網頁和窗體共享的通用源碼,在這個過程中推薦了幾遍相關的技術文檔。然后又針對導出Excel不同效果的分析與總結,結合第一遍實現上整天核心點已經講完。其他簡單的功能沒有在文章中贅述。下章主要介紹,在配置遠程連接數據庫遇到問題,以及如何解決問題,關於vpn和ftp是摸索中遇到的,到時候一並介紹。另外如何在服務器分享文件,控制文件讀寫權限。客戶端如何遠程連接服務器數據庫等關於配置安裝問題。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM