Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接silca(); 用于(整数计数器=0;计数器_C# - Fatal编程技术网

C# 连接silca(); 用于(整数计数器=0;计数器

C# 连接silca(); 用于(整数计数器=0;计数器,c#,C#,连接silca(); 用于(整数计数器=0;计数器

连接silca(); 用于(整数计数器=0;计数器<\u cnt1;计数器++) { fcl.cmd=新的OracleCommand( 选择图书id、图书标题、类别id、作者、图书发布、图书副本、出版商名称、isbn、版权年、添加日期、图书id='所在图书的状态+ listBox1.Items[counter].ToString()+“'”,fcl.con); var数据集=新数据集(); OracleDataAdapter OracleDataAdapter=新的OracleDataAdapter(cmd); oracleDataAdapter.Fill(数据集); _dataTables.Add(dataSet.Tables[0]); } } 其他的 { MessageBox.Show(“请至少选择一项”); } } 捕获(例外) { //捕获任何连接异常。 } 最后 { con.Close(); } } 私有void ExportToExcel() { //从该位置打开excel文件 //下面是现有的excel代码 常量字符串路径=@“C:\\EXCEL\\DATA\\myData.xls”; var excelApplication=new Application(){Visible=false,DisplayAlerts=false}; var workbook=excelApplication.Workbooks.Open(路径,0,false,5,“,”,false,XlPlatform.xlWindows,“,true,false,0,true,false,false); var工作表=工作簿。工作表[“输入”]作为工作表; 如果(工作表!=null) { 工作表.单元格[5,3]=10000089; 工作表.单元格[5,4]=001; workSheet.Cells[5,7]=System.DateTime.Now.ToString(“ddMMyyyy”); 工作表.单元格[5,10]=“ASHSBCTS2017”; 变量单元格=工作表单元格; var指数=12; foreach(数据表中的var表) { 对于(var r=0;r解决什么问题?这里只有一条语句和大量代码。我正在更新excel工作表,当我更新时,我正在为每个数据创建一个不同的excel工作表,例如我有10行数据集,它正在创建10个不同的excel文件,我希望所有数据导出到一个excel工作表中看看我对这个问题的回答,这可能会有帮助,但在这种情况下,数据从不同的excel文件导出到单个excel文件,在我的情况下,数据从oracle server导出到excel请查看我的代码一次,如果有任何解决方案,请给出
`private void button1_Click(object sender, EventArgs e)
    {
        if (cnt1 >= 1)
        {
            connectionSilca();
            for (int counter = 0; counter < cnt1; counter++)
            {
                fcl.cmd = new OracleCommand("select book_id,book_title,category_id,author,book_pub,book_copies,publisher_name,isbn,copyright_year,date_added,status from book where book_id='" + listBox1.Items[counter].ToString() + "'", fcl.con);

                DataTable dt = new DataTable();
                DataSet DS = new DataSet();
                OracleDataAdapter AD = new OracleDataAdapter(cmd);
                OracleDataAdapter da = new OracleDataAdapter(cmd);
                da.Fill(dt);
                da.Fill(DS);
                //DataTable temptable = DS.Tables[0];
                //ExportToExcel(temptable);
                ExportToExcel(DS.Tables[0]);
            }
        }
        else
        {
            MessageBox.Show("Please Select Atleast One Item");
        }
        con.Close();
        MessageBox.Show("EXCEL DOWNLOAD SUCCESSFULLY");
        datarefresh();
    }

    public void ExportToExcel(DataTable dtCollection)
    {
        var ds = dtCollection;
        Microsoft.Office.Interop.Excel.Worksheet objWorkSheet1 = null;
        Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application { Visible = false };
        Microsoft.Office.Interop.Excel.Workbooks objWorkbooks = objExcel.Workbooks;
        Microsoft.Office.Interop.Excel.Workbook objWorkbook = objWorkbooks.Add(Missing.Value);
        Microsoft.Office.Interop.Excel.Sheets objSheets = objWorkbook.Worksheets;
        Microsoft.Office.Interop.Excel.Range objCells;
        Microsoft.Office.Interop.Excel.Range myCell;

        //open the excel file form the location 
        //here is existing excel code
        string path = @"C:\\EXCEL\\DATA\\myData.xls";
        objExcel = new Microsoft.Office.Interop.Excel.Application();
        objExcel.DisplayAlerts = false;
        objWorkbook = objExcel.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
        //Get all the sheets in the workbook
        objSheets = objWorkbook.Worksheets;
        //Get the allready exists sheet
        objWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)objSheets.get_Item("Input");
        Microsoft.Office.Interop.Excel.Range range = objWorkSheet1.UsedRange;

        objWorkSheet1.Cells[5, 3]= 100000089;
        objWorkSheet1.Cells[5, 4] = 001;
        objWorkSheet1.Cells[5, 7] = System.DateTime.Now.ToString("ddMMyyyy");
        objWorkSheet1.Cells[5, 10] = "ASHSBCTS2017";

        var iCurrentRow = 12;
        var dt = ds;
        int columnsCount = dt.Columns.Count;
        objWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)(objSheets[1]);
        objCells = objWorkSheet1.Cells;

        /*for (var h = 0; h < dt.Columns.Count; h++)
        {
            myCell = (Microsoft.Office.Interop.Excel.Range)objCells[iCurrentRow, h + 1];
            myCell.Value2 = dt.Columns[h].ColumnName;
        }
        iCurrentRow++;*/
        for (var r = 0; r < dt.Rows.Count; r++)
        {
            for (var c = 0; c < dt.Columns.Count; c++)
            {
                    if (dt.Columns[c].DataType.Name == "String" || dt.Columns[c].DataType.Name == "DateTime")
                    {
                        myCell = (Microsoft.Office.Interop.Excel.Range)objCells[r + iCurrentRow, c + 1];
                        myCell.Value2 = "'" + dt.Rows[r][c].ToString().Trim();
                    }
                    else
                    {
                        myCell = (Microsoft.Office.Interop.Excel.Range)objCells[r + iCurrentRow, c + 1];
                        myCell.Value2 = dt.Rows[r][c];
                    }
                }
            }
        //objWorkSheet1.Cells.EntireRow.AutoFit();
        //objWorkSheet1.Cells.EntireColumn.AutoFit();
        objWorkbook.SaveAs("C:\\EXCEL\\DATA\\myData.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel12, System.Reflection.Missing.Value, 
            System.Reflection.Missing.Value, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, false, false,
            System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
        objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
        objExcel.Quit();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }`
objWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)objSheets.get_Item("Input");
objWorkSheet1 =  = objWorkbook.Worksheets["Input"] as Worksheet;
for (int counter = 0; counter < cnt1; counter++)
{
    .....
    ExportToExcel(DS.Tables[0]);
}
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Office.Interop.Excel;
using DataTable = System.Data.DataTable;

namespace ExcelExample
{
    internal class Test
    {
        private int _cnt1 = 0;
        private readonly List<DataTable> _dataTables = new List<DataTable>();

        private void button1_Click(object sender, EventArgs e)
        {
            ReadDataFromDatabase();
            ExportToExcel();
            DataRefresh();
        }

        private void ReadDataFromDatabase()
        {
            try
            {
                if (_cnt1 >= 1)
                {
                    _dataTables.Clear();
                    connectionSilca();
                    for (int counter = 0; counter < _cnt1; counter++)
                    {
                        fcl.cmd = new OracleCommand(
                            "select book_id,book_title,category_id,author,book_pub,book_copies,publisher_name,isbn,copyright_year,date_added,status from book where book_id='" +
                            listBox1.Items[counter].ToString() + "'", fcl.con);

                        var dataSet = new DataSet();

                        OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(cmd);
                        oracleDataAdapter.Fill(dataSet);
                        _dataTables.Add(dataSet.Tables[0]);
                    }
                }
                else
                {
                    MessageBox.Show("Please Select Atleast One Item");
                }
            }
            catch (Exception)
            {
                // catch any connection exceptions.
            }
            finally
            {
                con.Close();
            }
        }

        private void ExportToExcel()
        {
            //open the excel file form the location 
            //here is existing excel code
            const string path = @"C:\\EXCEL\\DATA\\myData.xls";
            var excelApplication = new Application(){Visible = false, DisplayAlerts = false};
            var workbook = excelApplication.Workbooks.Open(path, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            var workSheet = workbook.Worksheets["Input"] as Worksheet;

            if (workSheet != null)
            {
                workSheet.Cells[5, 3] = 100000089;
                workSheet.Cells[5, 4] = 001;
                workSheet.Cells[5, 7] = System.DateTime.Now.ToString("ddMMyyyy");
                workSheet.Cells[5, 10] = "ASHSBCTS2017";

                var cells = workSheet.Cells;
                var rowIndex = 12;

                foreach (var table in _dataTables)
                {
                    for (var r = 0; r < table.Rows.Count; r++)
                    {
                        for (var c = 0; c < table.Columns.Count; c++)
                        {
                            var currentCell = (Range)cells[rowIndex, c + 1];

                            if (table.Columns[c].DataType.Name == "String" || table.Columns[c].DataType.Name == "DateTime")
                            {
                                currentCell.Value2 = "'" + table.Rows[r][c].ToString().Trim();
                            }
                            else
                            {
                                currentCell.Value2 = table.Rows[r][c];
                            }
                        }
                        rowIndex++;
                    }
                }
            }

            workbook.SaveAs("C:\\EXCEL\\DATA\\myData.xls", XlFileFormat.xlExcel12, System.Reflection.Missing.Value,
                System.Reflection.Missing.Value, false, false, XlSaveAsAccessMode.xlShared, false, false,
                System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
            workbook.Close();
            excelApplication.Quit();

            MessageBox.Show("EXCEL DOWNLOAD SUCCESSFULLY");
        }
    }
}