将datagridview导出到无法通过c#打开的excel 2007(突然打开第二秒然后突然关闭) [英] Export datagridview to excel 2007 not open by c#(open suddenly for second then close suddenly)

查看:47
本文介绍了将datagridview导出到无法通过c#打开的excel 2007(突然打开第二秒然后突然关闭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作datagridview以显示数据,然后导出到excel 2007

I make datagridview to show data then export to excel 2007

在我的计算机上可以正常运行

in my computer it work ok no proplem

当我在另一台计算机上打开exe文件并按按钮导出数据时,gridview 转换为Excel

when i open exe file in another computer and press button export datagridview to excel

excel工作表打开一秒钟然后突然关闭,另一台计算机安装了Office 2007

excel sheet open for one second  and close suddenly and another computeres have office 2007

我的计算机也有2007年并且已打开,而另一台计算机没有中并且有2007年

and my computer also have 2007 and it open and in another computeres not open and have 2007

这是我的代码

使用  

using   

使用

使用

使用

使用

使用

使用

使用

使用

使用

  Microsoft.Office.Interop.Excel._Application应用=新的Microsoft.Office.Interop.Excel.Application();
  System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
  System.Threading.Thread.CurrentThread.CurrentCulture =新的System.Globalization.CultureInfo("zh-CN");
  Microsoft.Office.Interop.Excel._Workbook工作簿= app.Workbooks.Add(Type.Missing);
  Microsoft.Office.Interop.Excel._Worksheet工作表= null;
  app.Visible = true;
         
试试
{
                            
工作表=(Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets ["Sheet1"];
工作表=(Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
worksheet.Name =从Ketoan导出";
for(int i = 1; i< dataGridView1.Columns.Count +1; i ++)
{
worksheet.Cells [1,i] = dataGridView1.Columns [i-1] .HeaderText;
}
           
for(int i = 0; i< dataGridView1.Rows.Count-1; i ++)
{
  for(int j = 0; j< dataGridView1.Columns.Count; j ++)
  {
  worksheet.Cells [i + 2,j + 1] = dataGridView1.Rows [i] .Cells [j] .Value.ToString();
 }
 }

 Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
 System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
 System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
 Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
 Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
 app.Visible = true;
          
try
{
               
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
worksheet.Name = "Exported from Ketoan";
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
             
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
 for (int j = 0; j < dataGridView1.Columns.Count; j++)
 {
 worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
 }
 }

              
字符串fileName = String.Empty;
saveFileDialog1.Filter ="Excel文件| * .xlsx |所有文件(*.*)| *.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

               
string fileName = String.Empty;
saveFileDialog1.Filter = "Excel files |*.xlsx|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;


如果(saveFileDialog1.ShowDialog()== DialogResult.OK)
  {
fileName = saveFileDialog1.FileName;
                   
workbook.SaveAs(fileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing,Type.Missing,Type.Missing, Type.Missing,Type.Missing);


if (saveFileDialog1.ShowDialog() == DialogResult.OK)
 {
fileName = saveFileDialog1.FileName;
                   
workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

}
其他
返回;

}
else
return;

              
}
捕获(System.Exception ex)
{

               
}
catch (System.Exception ex)
{

}
终于
{
app.Quit();
workbook = null;
app = null;
}

}
finally
{
app.Quit();
workbook = null;
app = null;
}

在我的计算机中为我工作的代码

 the code working for me in my computer

但我的问题

为什么在另一台计算机中突然打开和关闭而不显示数据

Why it open and close suddenly in another computerES and not show data

以及如何解决这个问题

我的难题发生在更多计算机上,而不是一台计算机都安装了Office 2007

My proplem happen in more computeres not one computers all have office 2007

推荐答案

在可能的情况下,有人可以帮助我吗?
can any one help me in this question if possible


这篇关于将datagridview导出到无法通过c#打开的excel 2007(突然打开第二秒然后突然关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆