導出的Excel文件,打開時沒有內容,可是預覽或者全蘋顯示時有內容?

解决方案 »

  1.   

    //取得導出數據
    DataSet ds=dsSource;
    if(ds == null) 
    {
    return;
    } //彈出導出對話框文件設置
    string saveFileName = "";
    bool fileSaved=false;
    SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.DefaultExt  = "xls";
    saveDialog.Filter="Excel文件|*.xls";
    saveDialog.ShowDialog();
    saveFileName = saveDialog.FileName;
    if(saveFileName.IndexOf(":")<0) //取消返回
    {
    return; 
    } Excel.Application xlApp = new Excel.Application(); if(xlApp==null)
    {
    MessageBox.Show("無法創見Excel對象,請確定已安裝Excel軟是軟件!");
    return;
    } Excel.Workbooks workbooks=xlApp.Workbooks;
    Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
    Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
    Excel.Range range; string oldCaption=this.Text;
    long totalCount=ds.Tables[0].Rows.Count;
    long rowRead=0;
    float percent=0; worksheet.Cells[1,1]=this.Text;
    //寫入子段
    for(int i=0;i<ds.Tables[0].Columns.Count;i++)
    {
    worksheet.Cells[2,i+1]=ds.Tables[0].Columns[i].ColumnName;  
    range=(Excel.Range)worksheet.Cells[2,i+1];
    range.Interior.ColorIndex = 15;
    range.Font.Bold = true; }
    //寫入數值
    for(int r=0;r<ds.Tables[0].Rows.Count;r++)
    {
    for(int i=0;i<ds.Tables[0].Columns.Count;i++)
    {
    worksheet.Cells[r+3,i+1]=ds.Tables[0].Rows[r][i];     
    }
    rowRead++;
    percent=((float)(100*rowRead))/totalCount;    
    this.Text = "正在導出數據:["+ percent.ToString("0.00")  +"%]...";
    Application.DoEvents();
    }
    this.Text = oldCaption; range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[ds.Tables[0].Rows.Count+2,ds.Tables[0].Columns.Count]);
    range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null); range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin; if(ds.Tables[0].Columns.Count>1)
    {
    range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
    range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
    range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
    } if(saveFileName!="")
    {    
    try
    {
    workbook.Saved =true;   
    workbook.SaveCopyAs(saveFileName);
    fileSaved=true;
    MessageBox.Show("導出文件成功!");
    }
    catch(Exception ex)
    {
    fileSaved=false;
    MessageBox.Show("導出文件時出錯,文件可能正被打開!\n"+ex.Message);
    }
    }
    else
    {
    fileSaved=false;
    }   
    xlApp.Workbooks.Close();
    xlApp.Quit();
    GC.Collect();//強行銷毀 if(fileSaved && File.Exists(saveFileName)) 
    {
    System.Diagnostics.Process.Start(saveFileName);
    }
      

  2.   

    我是将它输出到页面再导出的StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter( stringWriter ); dgCountList.RenderControl(htmlWriter);
    string filePath = System.DateTime.Now.Year.ToString()
    + System.DateTime.Now.Month.ToString()
    + System.DateTime.Now.Day.ToString() + "-" 
    + System.DateTime.Now.Hour.ToString()
    + System.DateTime.Now.Minute.ToString()
    + System.DateTime.Now.Second.ToString()
    + System.DateTime.Now.Millisecond.ToString() + ".xls";//文件名
    string path = @"Log";
    string dir = Server.MapPath(path);
    string filestr = dir+@"\"+filePath; //filePath是文件的路径 if(!System.IO.Directory.Exists(dir))
    {
    System.IO.Directory.CreateDirectory(dir);
    } System.IO.StreamWriter sw = new StreamWriter(filestr);
    sw.Write(stringWriter.ToString());
    sw.Close(); this.RegisterStartupScript("suc","<script>window.open('log/"+filePath+"');</script>");dgCountList就是存放数据的那个datagrid.