导出为EXCEL

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;                     } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值