c#后台通过sql语句,如何把查出来的内容放在excel文件中

来源:百度知道 编辑:UC知道 时间:2024/05/29 15:25:35
c#后台通过sql语句,如何把查出来的内容放在excel文件中

有两种方法
1.excel本身就是个数据库,你可以操纵它就象操纵excel一样
2.我用的是2008,office2003,在工程里面先添加引用,浏览com,之后找到microsoft excel组建。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Excel.Application app = new ApplicationClass();
app.Visible = true;
Excel.Workbook wbk = app.Workbooks.Open(@"c:\Book1.xls", System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing);
Excel.Worksheet wst = (Worksheet)wbk.Worksheets[1];
wbk.SaveAs(@"c:\aa.txt",XlFileFormat.xlUnicodeText,
System.Type.Missing, System.Type.Missing, Syste