首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

大家看下 数据导出excel出现有关问题

2012-01-08 
大家看下 数据导出excel出现问题usingExcelusingSystem.DataprivateDataSetdsnewDataSet()privateData

大家看下 数据导出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();


进销存管理系统.util.GridUtil       gridutil=new   进销存管理系统.util.GridUtil();
gridutil.GridXls(sf,this.dgrd_StorageSearch,ExcelObj);
ExcelObj.Quit();
}


现在出现的问题是:
查询功能是可以的,但我导出是
出现(这条语句在导出按钮中)
System.Data.DataSet   ds   =   (System.Data.DataSet)this.dgrd_StorageSearch.DataSource;提示错误

未处理的“System.InvalidCastException”类型的异常出现在   进销存管理系统.exe   中。
其他信息:   指定的转换无效。



[解决办法]
无法将this.dgrd_StorageSearch.DataSource转换为DataSet,断点看一下是否为空引用或类型不匹配
[解决办法]
public bool ExportExcel(DataTable table,string strExcelFileName)
{

try
{
//object objOpt = Missing.Value;
Excel.Application excel = new Excel.Application();
excel.Visible = true;
Excel._Workbook wkb = excel.Workbooks.Add(true);
Excel._Worksheet wks = (Excel._Worksheet)wkb.ActiveSheet;

wks.Visible = Excel.XlSheetVisibility.xlSheetVisible;

int rowIndex=1;
int colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}

foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}

wkb.SaveAs(strExcelFileName,true,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);

return true;
}
catch
{
return false;
}
}
[解决办法]
上面不是有例子了吗?
楼主怎么不试试?
你先看看代码嘛,稍微改改就可以了的,你要首先了解原理
[解决办法]
即是一行一行的转换数据了
[解决办法]
带格式的导出
public void OpenExcelFile()
{
try
{
Excel.Application excelapp;
excelapp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook book = excelapp.Workbooks.Open(Application.StartupPath + @ "\ReportFile\018.xlt ",
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

Excel.Worksheet st1 = (Excel.Worksheet)book.Worksheets[1];

int rowBase = 3;
int colBase = 1;
int rowIndex = rowBase;
int colIndex = colBase;

foreach (DataRowView row in this.m_DataView)
{
if (!(bool)row[ "Selected "])
{
continue;
}

if(row[ "YunDanState "].ToString()!= "可取货 ")
{
continue;
}

Excel.Range range = (Excel.Range)st1.Rows[rowIndex + 1, Missing.Value];
range.Insert(Excel.XlInsertShiftDirection.xlShiftDown, Missing.Value);

st1.Cells[rowIndex, colIndex] = " ";
colIndex++;



st1.Cells[rowIndex, colIndex] = " ' " + row[ "YeWuLeiXings "].ToString();
colIndex++;

st1.Cells[rowIndex, colIndex] = " ' " + row[ "BeiZhu "].ToString();

rowIndex++;
colIndex = colBase;
}

excelapp.Visible = true;
}
catch (Exception ex)
{
Wip.Common.ShowExecResult(Wip.Common.GetExecResult(ex), false);
}
finally
{
Cursor.Current = Cursors.Default;
}
}

热点排行