如何使用Excellibrary中的Dialoguebox保存文件 [英] How to save file using Dialoguebox in Excellibrary

查看:111
本文介绍了如何使用Excellibrary中的Dialoguebox保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用excel表将数据从db导入excel表,它的工作正常,但问题是有一个固定的路径,我可以保存副本,但我的要求是我必须导入多个表单多个查询,所以我想使用savedialoguebox来保存这些文件的名称和位置不同,怎么可能?我的代码如下

  string  connectionstring =  null ; 
string sql = null ;
string data = null ;
int i = 0 ;
int j = 0 ;

Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;


xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet =(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item( 1 );

connectionstring = 数据源=(本地);初始目录= Report_Database;集成安全性=真正的;
cnn = new SqlConnection(connectionstring);
cnn.Open();
sql = SELECT * FROM Report_Tab;
SqlDataAdapter dscmd = new SqlDataAdapter(sql,cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);

for (i = 0 ; i < = ds.Tables [ 0 ]。Rows.Count - 1 ; i ++)
{
for (j = 0 ; j < = ds.Tables [ 0 ]。Columns.Count - 1 ; j ++)
{
data = ds.Tables [ 0 ]。行[i] .ItemArray [j] .ToString ();
xlWorkSheet.Cells [i + 1 ,j + 1 ] = data;
}
}

xlApp.ActiveWorkbook.SaveCopyAs( C :\\Users\\sajid\\Desktop\\test.xls);
xlApp.ActiveWorkbook.Saved = true ;
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);


MessageBox.Show( 创建的Excel文件,你可以找到文件d:\\Sam-informations.xls);

解决方案

从这里开始: http ://www.dotnetperls.com/savefiledialog [ ^ ]

i am trying to import data from db to excel sheet using excel sheet,its work fine,but problem is that there is a fixed path where i can save a copy,but my requirement is that i have to import multiple sheet base on multiple queries,so i want to use savedialoguebox to save these file with different name and different location,how is it possible?my code is as follow

string connectionstring = null;
                           string sql = null;
                           string data = null;
                           int i = 0;
                           int j = 0;

                           Microsoft.Office.Interop.Excel.Application xlApp;
                           Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                           Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                           object misValue = System.Reflection.Missing.Value;


                           xlApp = new Microsoft.Office.Interop.Excel.Application();
                           xlWorkBook = xlApp.Workbooks.Add(misValue);
                           xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                           connectionstring = "Data Source=(local);Initial Catalog=Report_Database;integrated security=True";
                           cnn = new SqlConnection(connectionstring);
                           cnn.Open();
                           sql = "SELECT * FROM Report_Tab";
                           SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
                           DataSet ds = new DataSet();
                           dscmd.Fill(ds);

                           for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                           {
                               for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                               {
                                   data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                                   xlWorkSheet.Cells[i + 1, j + 1] = data;
                               }
                           }

                           xlApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\sajid\\Desktop\\test.xls");
                           xlApp.ActiveWorkbook.Saved = true;
                           xlApp.Quit();

                           releaseObject(xlWorkSheet);
                           releaseObject(xlWorkBook);
                           releaseObject(xlApp);


                           MessageBox.Show("Excel file created , you can find the file D:\\Sam-informations.xls");

解决方案

Start here : http://www.dotnetperls.com/savefiledialog[^]


这篇关于如何使用Excellibrary中的Dialoguebox保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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