使用Windows窗体和Excel生成报告 [英] using windows forms and excel to generate a report

查看:70
本文介绍了使用Windows窗体和Excel生成报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我正在努力:

如果用户单击我在其中填充数据库的列表框中显示的信息,并且他们单击导出到excel"按钮,则程序应在电子表格中导出有关所选列表框项目的相关信息. br/>
代码,我们将不胜感激,但除此之外,任何信息都将对您有所帮助


注释中的OP代码:

Hello
I am struggling how to:

if a user click''s on information shown in the list box that i have populated my database with, and they click on a "export to excel" button the program should export relevant information about the selected list box item in a spreadsheet.

code will be appreciated but otherwise any info will be helpful


OP''s code from the comments:

private void button2_Click(object sender, EventArgs e) 
{
Excel.Application xlApp ; 
Excel.Workbook xlWorkBook ; 
Excel.Worksheet xlWorkSheet ; 
object misValue = System.Reflection.Missing.Value; 
xlApp = new Excel.ApplicationClass(); 
xlWorkBook = xlApp.Workbooks.Add(misValue); 
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

int i = 0; int j = 0; 
for (i = 0; i <= dataGridView1.RowCount - 1; i++) 
{ 
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++) 
{ 
DataGridViewCell cell = dataGridView1[j, i]; 
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value; 
} 
} 
xlWorkBook.SaveAs("csharp.net-informations.html", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
 
xlWorkBook.Close(true, misValue, misValue); 
xlApp.Quit(); releaseObject(xlWorkSheet); 
releaseObject(xlWorkBook); releaseObject(xlApp); 
MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");



[/编辑]

谁能帮我!

我的意思是



[/Edit]

can anyone help me please!

what i meant is

推荐答案

对您的代码有一些小疑问:

1.始终在try catch块中使用excel,因为它们很容易导致您无法控制的错误.

2.您应该使用xlWorkSheet.Cells [i + 1,j + 1] .Value = cell.Value.ToString(). excel单元格具有许多字段.Formula .Format等,您应始终指定要使用的选项,在某些情况下可能会产生错误.

我要说的最后一件事是,您还可以使用
xlWorkSheet =(Excel.Worksheet)xlWorkBook.Worksheets.Active;有些excel版本以三个空白表(我认为这是默认设置)开头,通常会获得第一个空白表.

还请始终在您使用的excel版本中包含dll文件,否则版本之间的细微差异会使它崩溃.[/编辑]

否则;快乐的编码;)
Some small commetns to your code:

1. Always use excel in a try catch block, they are prone to errors outside your control.

2. You should use xlWorkSheet.Cells[i + 1, j + 1].Value = cell.Value.ToString(). The excel cell has many fields .Formula .Format etc, you should always specify whitch one you use, and it can in some circumstances generate an error.

The last thing I would say is that you can also use
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Active; There are some excel versions that starts with three blank sheets (I think this is the default setting) active would normally get the first.

Also always include the dll file form the excel version you are using, small differences in versions can make it crash otherwise.[/Edit]

Otherwise; Happy coding ;)


这篇关于使用Windows窗体和Excel生成报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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