使用下面的代码我从数据库表导出excel中的数据。但是我想在sheet2中导出另一个表数据在同一个excel中是可能的 [英] With below code am exporting data in excel from Database table.But i want export another table data in same excel in sheet2 is it possible

查看:74
本文介绍了使用下面的代码我从数据库表导出excel中的数据。但是我想在sheet2中导出另一个表数据在同一个excel中是可能的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码从数据库表导出excel中的数据。但是我想在sheet2中的同一个excel中导出另一个表数据。



任何人都可以帮助我。





 私人  void  button1_Click( object  sender,EventArgs e)
{
SqlConnection cnn;
string connectionstring = null ;
string sql = null ;
string data = null ;
int i = 0 ;
int j = 0 ;

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


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

connectionstring = 数据源= HostName;初始目录= MSNETDB; Integrated Security = True;池=假;
cnn = new SqlConnection(connectionstring);
cnn.Open();

sql = SELECT * FROM SystemServiceInfo;
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.Visible = true ;





谢谢&重生

Sam.198979

解决方案

您好,



创建你的两个WorkSheets,从数据库中检索数据以填充它们,然后将这两个工作表添加到工作簿中,如下所示:

 xlWorkBook.Worksheets.Add(worksheet1)
xlWorkBook.Worksheets.Add(worksheet2)


With below code am exporting data in excel from Database table.But i want export another table data in same excel in sheet2 .

can any one help me.


private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn;
            string connectionstring = null;
            string sql = null;
            string data = null;
            int i = 0;
            int j = 0;
 
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
 

            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
 
            connectionstring ="Data Source=HostName;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
            cnn = new SqlConnection(connectionstring);
            cnn.Open();
            
            sql = "SELECT * FROM SystemServiceInfo";
            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.Visible = true;



Thanks & Regrades
Sam.198979

解决方案

Hello,

Create your two WorkSheets, retrieve data from database to fill them, and then add these two worksheets to the workbook like this :

xlWorkBook.Worksheets.Add(worksheet1)
xlWorkBook.Worksheets.Add(worksheet2)


这篇关于使用下面的代码我从数据库表导出excel中的数据。但是我想在sheet2中导出另一个表数据在同一个excel中是可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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