C# Winform Excel出力数据问题???
画面上 有个 按钮 ,我想点击 按钮 出力 一个 Excel 包含有 三个 Sheet 每个Sheet 出力 不同的 内容
求做法 ??????
直接 写 存储过程 抽出 好吗??
------解决方案--------------------可以写存储过程调用
出力是啥意思???
------解决方案--------------------对!把脑子理顺成中国的。
------解决方案--------------------
别人完整例子VS2008:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Runtime.InteropServices; //for comexcelption
using Excel;
using System.Reflection;
using System.IO;
using Microsoft.Office.Core;
namespace OutputExcel
{
class Program
{
public static SqlConnection getSqlConnection()
{
//第一种 :SQL 登陆模式
/*
String sqlCon = "server=(local);database=Practice;uid=sa;pwd=p9pip";
return new SqlConnection(sqlCon);
*/
//第二种:windows 登陆模式
String sqlCon = "server=(local);Integrated Security = SSPI;database=Practice";
return new SqlConnection(sqlCon);
}
SqlCommand cmd;
static void Main(string[] args)
{
SqlConnection con = Program.getSqlConnection();
con.Open();
//cmd = new SqlCommand("select * from dbo.Company11", con);
String strSql = "select * from dbo.Company11";
//第一种dataset(假如不知道列数)
SqlDataAdapter myDateAdapter = new SqlDataAdapter(strSql,con);//创建数据适配器
DataSet myDataSet = new DataSet();//建立dataset对象
myDateAdapter.Fill(myDataSet, "Table_Name"); //填充数据
int RowCount = myDataSet.Tables["Table_Name"].Rows.Count;
int ColCount = myDataSet.Tables["Table_Name"].Columns.Count;
//输出到黑屏
for (int indexRow = 0; indexRow < RowCount; indexRow++)
{
for (int indexCol = 0; indexCol < ColCount; indexCol++)
{
String Term = myDataSet.Tables["Table_Name"].Rows[indexRow][indexCol].ToString();
Console.Write("{0} ", Term);
}
Console.WriteLine();
}
//第二种 SqlDataReader (知道列数)
/*
cmd.Connection = con;
cmd.CommandText = strSql;
int RowCount = cmd.ExecuteNonQuery();//影响的行数
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
// Console.WriteLine(sdr["CompanyID"] + "________" + sdr["CmpanyName"] + "________" + sdr["createdate"]);
Console.WriteLine("{0} -- {1} -- {2}",sdr["CompanyID"],sdr["CmpanyName"],sdr["createdate"]);
}
//关闭reader
if(sdr!=null)
sdr.Close();
*/
Excel.Application excelApp = new Excel.ApplicationClass();//创建excel对象
Excel.Workbook workBook = excelApp.Workbooks.Add(true); //新建工作薄
Excel.Worksheet workSheet = workBook.ActiveSheet as Excel.Worksheet; //新建工作表
Excel.Range titleRange = workSheet.get_Range( //设置标题
workSheet.Cells[1, 1], workSheet.Cells[1, ColCount]); //选取单元格
titleRange.Merge(true); //合并单元格
titleRange.Value2