前言
在开发人力资源项目的时候,一开始只是为了实现功能而实现功能,对于性能方面不是说没有考虑到,只能先保证一个系统稳定运行,然后后期进行优化。就这样一边开发,一边优化,进行的不亦乐乎···
内容
前天由于项目中需要一下子导出大概上千条数据,清楚的记得米老师那天晚上验收的时候,心里很是惶恐,做的自以为傲的东西,就在导出数据这块,时间稍微有点长了,米老师指出,这块性能确实可以优化一下。想着想着,就开始行动了,就这样欢欢有一个方案,我也有一个,然后我们一起行动。下面介绍一些我的优化方案:
需求:
政府公司需要对本公司录入的表的数据进行加密导出到Excel中。(每个公司最多填写7张表)按最多的来,七张表大概是数据。
一开始是这样做的:
加密函数(伟大的攻坚小组的成果)
<span style="font-family:KaiTi_GB2312;font-size:18px;">'***************** '定义一个加密函数* '***************** Public Function EncryptDes(ByVal SourceStr As String, ByVal myKey As String, ByVal myIV As String) As String '使用的DES对称加密 Try Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法 'Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider'TripleDES算法 Dim inputByteArray As Byte() 'SourceStr = "" inputByteArray = System.Text.Encoding.Default.GetBytes(SourceStr) des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符,TripleDES要24个字符 des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符,TripleDES要24个字符 Dim ms As New System.IO.MemoryStream Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write) Dim sw As New System.IO.StreamWriter(cs) sw.Write(SourceStr) sw.Flush() cs.FlushFinalBlock() ms.Flush() EncryptDes = Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length) Catch ex As Exception MessageBox.Show(ex.Message.ToString()) End Try End Function</span>
D层调用Data表
<span style="font-family:KaiTi_GB2312;font-size:18px;"> '返回data表的方法 Public Function ReturnData() As DataTable Dim sql As String Dim table As DataTable sql = "select * from T_dataTable " table = helper.ExecSelect(sql, CommandType.Text) Return table End Function</span>
B层获取D层值并且给D层传参
<span style="font-family:KaiTi_GB2312;font-size:18px;"> '返回data表的方法 Public Function ReturnData() As DataTable Dim simpleQuerydal As New DAL.SimpleQueryDAL Dim table As DataTable table = simpleQuerydal.ReturnData() Return table End Function</span>
U层遍历DataGridView,将该表传给Excel
<span style="font-family:KaiTi_GB2312;font-size:18px;"> '导出数据表 tabledata = simplebll.ReturnData() DataData.DataSource = tabledata Dim MyExcel As New Microsoft.Office.Interop.Excel.Application() '定义并实例化excel工作表 MyExcel.Application.Workbooks.Add(True) '打开excel工作簿,显示excel界面 For Col = 0 To DataData.ColumnCount - 1 'col的变化范围 MyExcel.Cells(1, Col + 1) = Me.DataData.Columns(Col).HeaderText '添加标题 Next Col Dim k As Integer '定义整型行变量 i Dim j As Integer '定义整型列变量 j For k = 0 To DataData.RowCount - 2 For j = 0 To DataData.ColumnCount - 1 '列变量取值范围 If Me.DataData(j, k).Value IsNot System.DBNull.Value Then Dim EDes As String EDes = EncryptDes(DataData(j, k).Value, "12345678", "abcdefgh") MyExcel.Cells(k + 2, j + 1) = EDes End If Next j Next k MyExcel.ActiveWorkbook.SaveAs(path + "数据表" + _companyName & Format(Now, "yy-MM-dd") & ".xls", ReadOnlyRecommended:=False) MyExcel.Quit() picProcess.Visible = False MsgBox("导出到桌面成功!", , "提示") Catch ex As Exception End Try</span>
数据量为
导出所用时间:
240S
优化后的结果(其他层不变)
U层
定义一个数组“Array”,将DataGridView值直接赋值给Excel。
<span style="font-family:KaiTi_GB2312;font-size:18px;">'导出数据表 tabledata = simplebll.ReturnData() DataData.DataSource = tabledata Dim MyExcel As New Microsoft.Office.Interop.Excel.Application() '定义并实例化excel工作表 MyExcel.Application.Workbooks.Add(True) '打开excel工作簿,显示excel界面 Dim arr3 As Array = Array.CreateInstance(GetType(String), DataData.RowCount, DataData.ColumnCount) For Col = 0 To DataData.ColumnCount - 1 'col的变化范围 MyExcel.Cells(1, Col + 1) = Me.DataData.Columns(Col).HeaderText '添加标题 Next Col Dim k As Integer '定义整型行变量 i Dim j As Integer '定义整型列变量 j For k = 0 To DataData.RowCount - 2 For j = 0 To DataData.ColumnCount - 1 If IsDBNull(DataData.Rows(k).Cells(j).Value.ToString) Or IsNothing(DataData.Rows(k).Cells(j).Value.ToString) Then datavalue3 = 0 Else datavalue3 = DataData.Rows(k).Cells(j).Value.ToString End If arr3.SetValue(datavalue3, k, j) Next Next Dim EDes3 As String EDes3 = EncryptDes(arr3.GetValue(DataData.RowCount - 2, DataData.ColumnCount - 1), "12345678", "abcdefgh") Dim oRang3 As Microsoft.Office.Interop.Excel.Range oRang3 = MyExcel.Range(MyExcel.Cells(2, 1), MyExcel.Cells(DataData.RowCount, DataData.ColumnCount)) oRang3.Value = EDes3 MyExcel.ActiveWorkbook.SaveAs(path + "数据表" + _companyName & Format(Now, "yy-MM-dd") & ".xls", ReadOnlyRecommended:=False) MyExcel.Quit() picProcess.Visible = False MsgBox("导出到桌面成功!", , "提示")</span>
用时为:
7S
不知道为什么加密这里有点小问题(这个后期会再完善),但是在导出的数据量相等的情况下,还是用后者更适合需求 ,适合用户。马云说:客户第一。米老师说:客户第一。
小结
1、不要相信你眼前的就是最好的,没有最好的,只有最好的。
2、开始优化性能的道路···
感谢您的宝贵时间···