导出Excel:
//export data
        private void m_ExportBT_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();                saveFileDialog.Filter = "Excel (*.XLS)|*.xls";
                saveFileDialog.FilterIndex = 2;
                saveFileDialog.RestoreDirectory = true;                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    ToExcel(m_dataGrid, saveFileDialog.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        public static bool ToExcel(DataGridView view, string fileName)
        {
            try
            {
                File.Delete(fileName);
           
                FileStream objFileStream;
                StreamWriter objStreamWriter;
            
                string strLine = "";
                
                objFileStream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write);
                objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
                //objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.ASCII);                strLine = "";
                for (int i = 0; i < view.Columns.Count; i++)
                {
                    strLine = strLine + view.Columns[i].HeaderText.ToString() + Convert.ToChar(9);
                }                objStreamWriter.WriteLine(strLine);
                strLine = "";                //objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.ASCII);
                for (int i = 0; i < view.Rows.Count; i++)
                {
                    for (int j = 0; j < view.Columns.Count; j++)
                    {
                        strLine = strLine + view.Rows[i].Cells[j].FormattedValue.ToString() + Convert.ToChar(9);
                    }
                    objStreamWriter.WriteLine(strLine);
                    strLine = "";
                }                objStreamWriter.Close();
                objFileStream.Close();
            }
            catch (Exception e)
            {
                //throw new Exception(e.Message);
                MessageBox.Show("该文件可能已打开,请先关闭该文件!");
            }            return true;
        }
应该说这两个功能速度上还过的去,特别是导出功能.但问题是我导出的Excel文本却导不进去了,是文件结构改变了还是怎么的,请高人指点!谢谢!!!

解决方案 »

  1.   

    private void btnOpen_Click(object sender, System.EventArgs e)
    {
    //选择导入的文件,并将其导入到DataSet中
    strFileInfo="";
    this.dataGrid1.DataSource=null;
    this.openFileDialog1.InitialDirectory=clsCommon.GetSavePath();
    this.openFileDialog1.Filter="Excel File(*.xls)|*.xls";
    if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
    strFileInfo=openFileDialog1.FileName.ToLower();
    Application.DoEvents();
    if(strFileInfo=="") return;
    //初始化查询语句
    string strsql1="";
    string strsql="";
    strsql=strsql + "Select\n";
    //strsql=strsql + " 発注日付,\n";
    strsql=strsql + " Sun,\n";
    strsql=strsql + " Mon,\n";
    strsql=strsql + " Tue,\n";
    strsql=strsql + " Wed,\n";
    strsql=strsql + " Thu,\n";
    strsql=strsql + " Fri,\n";
    strsql=strsql + " Sat,\n";
    strsql=strsql + " 休止開始日,\n";
    strsql=strsql + " 休止最終日,\n";
    strsql=strsql + " 売上休止フラグ\n";
    strsql=strsql + "From\n";
    //显示等待画面
    int[] intp=new int[2]{0,0};
    intp[0]=this.Left + (this.Width-400)/2;
    intp[1]=this.Top + (this.Height-80)/2;
    clsCommon.SetFrmPosition(intp);
    frmWait frmW=new frmWait();
    frmW.Show();
    Application.DoEvents();
    //读取Excel数据
    string strodcn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileInfo + ";Extended Properties=\"Excel 8.0;MaxScanRows=8\"";
    OleDbConnection odcn=new OleDbConnection(strodcn);
    try
    {
    odcn.Open();
    }
    catch(Exception ex)
    {
    frmW.Close();
    MessageBox.Show(ex.Message);
    return;
    }
    strsql1=strsql + " [データ2$]\n";
    strsql1=strsql1 + "Where\n";
    strsql1=strsql1 + " 更新フラグ>0";
    OleDbDataAdapter oddp=new OleDbDataAdapter(strsql1,odcn);
    DataSet ds=new DataSet();
    try
    {
    oddp.Fill(ds,"データ");
    strsql1=strsql + " [データ1$]\n";
    strsql1=strsql1 + "Where\n";
    strsql1=strsql1 + " 更新フラグ>0";
    oddp.SelectCommand.CommandText=strsql1;
    oddp.Fill(ds,"データ");
    //ds.Tables["データ"].PrimaryKey=new DataColumn[] {ds.Tables["データ"].Columns["商品CD"]};
    }
    catch
    {
    try
    {
    strsql1=strsql + " [データ1$]\n";
    strsql1=strsql1 + "Where\n";
    strsql1=strsql1 + " 更新フラグ>0";
    oddp.SelectCommand.CommandText=strsql1;
    oddp.Fill(ds,"データ");
    //ds.Tables["データ"].PrimaryKey=new DataColumn[] {ds.Tables["データ"].Columns["商品CD"]};
    }
    catch
    {
    frmW.Close(); MessageBox.Show("選択してください。");
    return;
    }
    }
    dsXls=ds;
    dsXls.CaseSensitive=false;
    ds=null;
    oddp=null;
    odcn.Close();
    //显示数据

    this.dataGrid1.SetDataBinding(dsXls,"データ");
    DataGridTableStyle ts=new DataGridTableStyle();
    ts.MappingName="データ";
    try
    {
    this.dataGrid1.TableStyles.Add(ts);
    this.dataGrid1.TableStyles["データ"].AllowSorting=false;
    this.dataGrid1.TableStyles["データ"].GridColumnStyles["商品CD"].Width=100;
    }
    catch{}
    frmW.Close();
    if(dsXls.Tables["データ"].Rows.Count>0)
    {
    //MessageBox.Show("抽出データが"+  "0件です。");
    this.btnUpdate.Enabled=true;

    }
    else
    {
    this.btnUpdate.Enabled=false;
    MessageBox.Show("抽出データは0件です。");
    }
    }