日期:2014-05-19  浏览次数:20766 次

大家看下 数据导出excel出现问题
using   Excel;
using   System.Data;

private   DataSet   ds   =   new   DataSet();
private   DataView   dv;
private   LinkDataBase   link   =   new   LinkDataBase();
private   string   sendTableName   =   "aa ";
private   string   sendStrSQL   =   "select   distinct   销售单号,名称,销售日期,金额   from   aa   order   by   销售日期 ";
private   System.Windows.Forms.DataGrid   dgrd_StorageSearch;


public   shoukuan()
{
//
//   Windows   窗体设计器支持所必需的
//
InitializeComponent();
this.selectDataBase();

//
//   TODO:   在   InitializeComponent   调用后添加任何构造函数代码
//

private   void   selectDataBase()
{
this.ds   =   this.link.SelectDataBase(sendStrSQL,sendTableName);
this.dv   =   new   DataView(ds.Tables[0]);
this.dgrd_StorageSearch.DataSource   =   dv;
}


查询按钮
private   void   btn_Search_Click(object   sender,   System.EventArgs   e)
{
string   strRowFilter   =   " ";
string   str1= "销售单号   like   ' "   +   txt1.Text.Trim()   +   "% ' ";
string   str2= "名称   like   ' "   +   txt2.Text.Trim()   +   "% ' ";
string   strWareNumFilter   = "销售日期   > =   ' "   +   txt_WareNum.Text.Trim()+   "% ' ";
string   strWareWordFilter   = "销售日期   <=   ' "   +   txt_WareWord.Text.Trim()   +   "% ' ";

if   (txt1.Text.Trim()   !=   " ")
strRowFilter   +=   str1   +   "   and   ";
if   (txt2.Text.Trim()   !=   " ")
strRowFilter   +=   str2   +   "   and   ";
if   (txt_WareNum.Text.Trim()   !=   " ")
strRowFilter   +=   strWareNumFilter   +   "   and   ";
if   (txt_WareWord.Text.Trim()   !=   " ")
strRowFilter   +=   strWareWordFilter   +   "   and   ";
if   (strRowFilter   !=   " ")     //   存在查询条件
strRowFilter   =   strRowFilter.Substring(0,strRowFilter.Length-5);
dv.RowFilter   =   strRowFilter;
this.txt1.Text   =   " ";
this.txt2.Text   =   " ";
this.txt_WareWord.Text   =   " ";
this.txt_WareNum.Text   =   " ";
}

导出excel按钮
private   void   button1_Click(object   sender,   System.EventArgs   e)
{
System.Data.DataSet   ds   =   (System.Data.DataSet)                                                 this.dgrd_StorageSearch.DataSource;

if(ds==null||   ds.Tables.Count <=0)  
{
MessageBox.Show( "您没查询或导入数据表,不能导出Excel!!!\n       请进行查询或导入数据表!! ");
return;
}
Excel.Application   ExcelObj   =   null;
ExcelObj   =   new   Excel.Application();
ExcelObj.Visible   =false;
SaveFileDialog   sf   =   new   SaveFileDialog();