public void excelout(string 查询语句, string 标题) { Microsoft.Office.Interop.Excel._Application excel; Microsoft.Office.Interop.Excel._Workbook xbk; Microsoft.Office.Interop.Excel._Worksheet xst; Microsoft.Office.Interop.Excel._QueryTable xqt; string conn = "odbc;driver=sql server;Server=localhost; Uid=sa;pwd=sa; database=byproduct"; string select = 查询语句; DataSet ds = new DataSet(); SqlConnection ck = new SqlConnection("Server=localhost; Uid=sa;pwd=sa; database=byproduct"); SqlCommand uu = new SqlCommand("select field1,date as 更新日期,field2,field3,field4,total as 累计, 单位 from sale_store order by ID desc", ck); SqlDataAdapter da1 = new SqlDataAdapter(uu); ck.Open(); da1.Fill(ds); ck.Close(); excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); int iRows = ds.Tables[0].Rows.Count; xbk = excel.Workbooks.Add(true); xst = (Microsoft.Office.Interop.Excel._Worksheet)xbk.ActiveSheet; excel.Cells[1, 3] = 标题; xst.get_Range("A2", "H" + iRows).RowHeight = 30; xst.get_Range("A2", "G" + 2).AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2, true, true, false, false, true, false); xst.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Bold = true; xst.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Name = "黑体"; xst.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Size = 22; xst.get_Range(excel.Cells[1, 1], excel.Cells[iRows + 2, 9]).Font.Size = 18; xqt = xst.QueryTables.Add(conn, xst.get_Range("A2", "G" + iRows), select); xqt.Name = "导出示例"; xqt.FieldNames = true; xqt.RowNumbers = false; xqt.FillAdjacentFormulas = false; xqt.PreserveFormatting = true; xqt.BackgroundQuery = true; xqt.RefreshStyle = Microsoft.Office.Interop.Excel.XlCellInsertionMode.xlInsertDeleteCells; xqt.AdjustColumnWidth = true; xqt.RefreshPeriod = 0; xqt.PreserveColumnInfo = true; xqt.Refresh(xqt.BackgroundQuery); excel.ActiveWindow.Visible = true; excel.Visible = true; }
导出为EXCEL
最新推荐文章于 2025-05-07 09:57:20 发布