using System;
using Excel;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
namespace ExcelLib
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class ExcelOper
{
public ExcelOper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//写内容到Excel文档中(根据指定的路径直接写入)
public void ExecuteWriteExcel()
{
string strPath="";
// string strMonth="";
//创建一个Excel文件
Excel.Application myExcel=new Excel.Application();
//实例化一个工作簿
Excel.Workbook oBook;
//Excel表
Excel.Sheets oSheets ;
//工作表
Excel.Worksheet oSheet;
//
Excel.Range oCells;
//当前运行Excel应用程序中加入工作簿
oBook=myExcel.Application.Workbooks.Add(true);
//工作表
oSheets=oBook.Worksheets;
oSheet=(Excel.Worksheet)oSheets[1];
oCells=oSheet.Cells ;
//让excel不可见
myExcel.Visible=false;
myExcel.DisplayAlerts = false;
//第一行为报表名称
oCells.Cells[1,4]=DateTime.Now.Year+"年"+DateTime.Now.Month.ToString()+"月";
///读取pubs数据库中的titles表,并将读取的数据写入Excel中
DataSet dstReport = new DataSet();
dstReport = GetTitlesData();
//逐行写入报表
System.Data.DataTable dtReport=dstReport.Tables[0];
//逐行写入报表
//打印列名 注意第一和最后一个不转换打印
for(int i=0;i<=dtReport.Columns.Count-2;i++)
{
//将第一个和最后一个不转换
if (dtReport.Columns[i+1].ToString()=="合计")
myExcel.Cells[2,i+2]=dtReport.Columns[i+1].ToString();
// else
// myExcel.Cells[2,i+2]=oPeopleManage.GetPeople(dtReport.Columns[i+1].ToString()).xm;
myExcel.Cells[2,i+2]=dtReport.Rows[0][i+1].ToString();
}
//打印内容,最后一行列合计不转换
for(int i=0;i<=dtReport.Rows.Count-1;i++)
{ //注意每行的第一个要转换,合计除外
// if (dtReport.Rows[i]["f0"].ToString().Trim()=="合计")
// myExcel.Cells[i+3,1]="合计";
// else //将fo字段的项目no转成项目名称
// myExcel.Cells[i+3,1]=oXmbManage.FindXmb(Convert.ToInt16(dtReport.Rows[i]["f0"].ToString().Trim())).xmmc;
for(int j=1;j<=dtReport.Columns.Count-1;j++)//从第二列开始
{
myExcel.Cells[i+3,j+1]=dtReport.Rows[i][dtReport.Columns[j].ToString()];
}
}
///
//写入Excel文件的路径
strPath="d://gs.xls";
//如果存在该文件则删除
if (File.Exists(strPath))
{
File.Delete(strPath);
}
//保存该文件
oSheet.SaveAs(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
// oBook.Close(Missing.Value,strPath,f);
myExcel.Quit();
//释放COM引用资源
Marshal.ReleaseComObject(oCells);
oCells=null;
Marshal.ReleaseComObject(oSheet);
oSheet=null;
Marshal.ReleaseComObject(oSheets);
oSheets=null;
Marshal.ReleaseComObject(oBook);
oBook=null;
Marshal.ReleaseComObject(myExcel);
myExcel=null;
//控制系统垃圾回收器,回收垃圾
System.GC.Collect();
}
//写内容到Excel文档中(根据选择的路径写入)
public void ExecuteWriteExcelSel()
{
string strPath="";
// string strMonth="";
//创建一个Excel文件
Excel.Application myExcel=new Excel.Application();
myExcel.Application.Workbooks.Add(true);
//让excel不可见
myExcel.Visible=false;
myExcel.EnableAutoComplete=true;
// myExcel.DisplayCommentIndicator=false;
myExcel.DisplayExcel4Menus=false;
myExcel.DisplayNoteIndicator=false;
myExcel.DisplayAlerts = false;
//第一行为报表名称
myExcel.Cells[1,4]=DateTime.Now.Year+"年"+DateTime.Now.Month.ToString()+"月";
//注意文件的格式200607gs.xls
strPath="d://gs.xls";
if (File.Exists(strPath)) //如果存在该文件则删除
{
File.Delete(strPath);
}
myExcel.DisplayAlerts = false;
myExcel.SaveWorkspace(strPath);
myExcel.Save(strPath);
myExcel.DisplayAlerts = false;
myExcel.Quit();
System.GC.Collect();
}
//查询titles表
public DataSet GetTitlesData()
{
string strSQL = "select * from titles";
DataSet ds = new DataSet();
try
{
ds = dbaccess.RunSelect(strSQL);
}
catch(Exception ex)
{
// return;
}
return ds;
}
//从数据库中读取数据,写入到Excel文件中
public void WriteExcel()
{
string strPa ="";
//实例化一个Excel程序
Excel.Application ex = new Excel.Application();
//工作簿
Excel.Workbook oBook;
//工作表集
Excel.Sheets oSheets;
//工作表
Excel.Worksheet oSheet;
//工作表中单元格
Excel.Range oCells;
//当前工作簿集中加入工作簿
oBook = ex.Application.Workbooks.Add(true);
//得到当前工作簿中的工作表集
oSheets = oBook.Worksheets;
//得到当前工作表1
oSheet = (Excel.Worksheet)oSheets[1];
//当前工作表的单元格集合
oCells = oSheet.Cells;
//隐藏Excel应用程序
ex.Visible = false;
//
ex.DisplayAlerts = false;
//当前工作表中第一行第一列中写入当前年月信息的字符串
oCells.Cells[1,1] = DateTime.Now.Year + "年" + DateTime.Now.Month + "月";
//指定要写入的Excel文件的路径
strPa = "C://Documents and Settings//Administrator//桌面//xx.xls";
//假如该路径下面的文件名与要保存的文件名同名,则需删掉原文件
if(File.Exists(strPa))
{
File.Delete(strPa);
}
//保存已完成写入操作的Excel报表
oSheet.SaveAs(strPa,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
//退出当前Excel程序
ex.Quit();
//释放COM引用的对象
Marshal.ReleaseComObject(oCells);
oCells = null;
Marshal.ReleaseComObject(oSheet);
oSheet = null;
Marshal.ReleaseComObject(oSheets);
oSheets = null;
Marshal.ReleaseComObject(oBook);
oBook = null;
Marshal.ReleaseComObject(ex);
ex = null;
//调用系统的垃圾回收器,清理内存垃圾
System.GC.Collect();
}
}
}