C#导数带格式EXCEL
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
if (app == null)
return;
app.Visible = false;
app.UserControl = true;
Excel.Workbooks workbooks = app.Workbooks;
Excel._Workbook workbook = workbooks.Add(@"C:\Model.xls");//
Excel.Sheets sheets = workbook.Worksheets;
Excel._Worksheet worksheet = (Excel._Worksheet)sheets.get_Item(1);
if (worksheet == null)
return;
DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocalTransaction, "select * from [Sheet1$]");
DataSet ds2 = SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocalTransaction, "select 支行,sum(交易金额) from [sheet1$] group by 支行");
int j = 0;
int i = 0;
double total = 0;
if (ds2.Tables[0].Rows.Count > 0)
{
DataRow []ds3;
for(i=0;i<ds2.Tables[0].Rows.Count;i++)
{
ds3=ds.Tables[0].Select("支行='"+ds2.Tables[0].Rows[i][0]+"'");
for (int k=0; k < ds3.Length ; k++)
{
worksheet.Cells[2 + j, 1] = ds3[k]["商户编号"].ToString();
worksheet.Cells[2 + j, 2] = ds3[k]["交易金额"].ToString();
worksheet.Cells[2 + j, 3] = ds3[k]["支行"].ToString();
worksheet.Cells[2 + j, 4] = ds3[k]["奖励标准"].ToString();
worksheet.Cells[2 + j, 5] = ds3[k]["备注"].ToString();
j++;
}
//汇总信息
worksheet.Cells[2 + j, 2] = ds2.Tables[0].Rows[i][1].ToString() ;
total += Convert.ToDouble(ds2.Tables[0].Rows[i][1]);
worksheet.Cells[2 + j, 3] = ds2.Tables[0].Rows[i][0].ToString() + " 汇总";
//下一次填充开始位置
j++;
}
worksheet.Cells[2 + j, 2] = total;
worksheet.Cells[2 + j, 3] = "总计";
}
workbook.SaveAs("C:\\aa.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
app.Quit();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
if (app == null)
return;
app.Visible = false;
app.UserControl = true;
Excel.Workbooks workbooks = app.Workbooks;
Excel._Workbook workbook = workbooks.Add(@"C:\Model.xls");//
Excel.Sheets sheets = workbook.Worksheets;
Excel._Worksheet worksheet = (Excel._Worksheet)sheets.get_Item(1);
if (worksheet == null)
return;
DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocalTransaction, "select * from [Sheet1$]");
DataSet ds2 = SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocalTransaction, "select 支行,sum(交易金额) from [sheet1$] group by 支行");
int j = 0;
int i = 0;
double total = 0;
if (ds2.Tables[0].Rows.Count > 0)
{
DataRow []ds3;
for(i=0;i<ds2.Tables[0].Rows.Count;i++)
{
ds3=ds.Tables[0].Select("支行='"+ds2.Tables[0].Rows[i][0]+"'");
for (int k=0; k < ds3.Length ; k++)
{
worksheet.Cells[2 + j, 1] = ds3[k]["商户编号"].ToString();
worksheet.Cells[2 + j, 2] = ds3[k]["交易金额"].ToString();
worksheet.Cells[2 + j, 3] = ds3[k]["支行"].ToString();
worksheet.Cells[2 + j, 4] = ds3[k]["奖励标准"].ToString();
worksheet.Cells[2 + j, 5] = ds3[k]["备注"].ToString();
j++;
}
//汇总信息
worksheet.Cells[2 + j, 2] = ds2.Tables[0].Rows[i][1].ToString() ;
total += Convert.ToDouble(ds2.Tables[0].Rows[i][1]);
worksheet.Cells[2 + j, 3] = ds2.Tables[0].Rows[i][0].ToString() + " 汇总";
//下一次填充开始位置
j++;
}
worksheet.Cells[2 + j, 2] = total;
worksheet.Cells[2 + j, 3] = "总计";
}
workbook.SaveAs("C:\\aa.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
app.Quit();
}
}
}