Can I import INTO excel from a data source without iteration?

I currently have an application that takes information from an SQLite database and puts it into Excel. However, I need to take each DataRow, iterate over each element and put each value in its own cell and determine the selection. It takes 20 minutes to export 9000 records to Excel. I am sure that this can be done faster. My thoughts is that I can use a data source to populate an Excel range, and then use column headings and row numbers to format only those rows that need to be formatted. However, when I look online, no matter what I seem to print, it always shows examples of using Excel as a database, nothing about importing into excel. If I do not forget the keyword or.This function should now be executed in code as part of a larger application. Otherwise, I would simply connect Excel to the database and pull out the information myself. Unfortunately, it is not. Any information that could help me in quickly loading an excel sheet would be greatly appreciated. Thank.

Additional information:
Another reason why the extraction of information from the database should be performed in the code is that not every computer on which it is loaded will have Excel in the subject. The person using the application may simply ask you to export the data and send it to your supervisor. The configuration application contains the necessary DLLs for the application to make the correct format.

Code example (current):

    For Each strTemp In strColumns
        excelRange = worksheet.Cells(1, nCounter)
        excelRange.Select()
        excelRange.Value2 = strTemp
        excelRange.Interior.Color = System.Drawing.Color.Gray.ToArgb()
        excelRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Type.Missing)
        nCounter += 1
    Next

, . , , DataTable, dataRow , ; , , , ( ), . excel (A2:??, , ), . , , , .


excelRange = worksheet.Cells("A2", "P9000")
excelRange.DataSource = ds 'ds would be a queried dataSet, and I know there is no excelRange.DataSource.
'Iteration code to format cells

:

, VB, , VB , VB. , , Recordset. ConvertToRecordset .

        private void CreatePartSheet(Excel.Worksheet excelWorksheet)
        {
            _dataFactory.RevertDatabase();
            excelWorksheet.Name = "Part Sheet";
            string[] strColumns = Constants.strExcelPartHeaders;
            CreateSheetHeader(excelWorksheet, strColumns);

            System.Drawing.Color clrPink = System.Drawing.Color.FromArgb(203, 192, 255);
            System.Drawing.Color clrGreen = System.Drawing.Color.FromArgb(100, 225, 137);

            string[] strValuesAndTitles = {/*...Column Names...*/};

            List<string> lstColumns = strValuesAndTitles.ToList<string>();

            System.Data.DataSet ds = _dataFactory.GetDataSet(Queries.strExport);
            ADODB.Recordset rs = ConvertToRecordset(ds.Tables[0]);
            excelRange = excelWorksheet.get_Range("A2", "ZZ" + rs.RecordCount.ToString());
            excelRange.Cells.CopyFromRecordset(rs, rs.RecordCount, rs.Fields.Count);
            int nFieldCount = rs.Fields.Count;

            for (int nCounter = 0; nCounter < rs.RecordCount; nCounter++)
            {
                int nRowCounter = nCounter + 2;
                List<ReportRecord> rrPartReports = _lstReports.FindAll(rr => rr.PartID == nCounter).ToList<ReportRecord>();
                excelRange = (Excel.Range)excelWorksheet.get_Range("A" + nRowCounter.ToString(), "K" + nRowCounter.ToString());
                excelRange.Select();
                excelRange.NumberFormat = "@";

                if (rrPartReports.Count > 0)
                {
                    excelRange.Interior.Color = System.Drawing.Color.FromArgb(230, 216, 173).ToArgb(); //Light Blue

                    foreach (ReportRecord rr in rrPartReports)
                    {
                        if (lstColumns.Contains(rr.Title))
                        {
                            excelRange = (Excel.Range)excelWorksheet.Cells[nRowCounter, lstColumns.IndexOf(rr.Title) + 1];
                            excelRange.Interior.Color = rr.Description.ToUpper().Contains("TAG") ? clrGreen.ToArgb() : clrPink.ToArgb();

                            if (rr.Description.ToUpper().Contains("TAG"))
                            {
                                rs.Find("PART_ID=" + (nCounter + 1).ToString(), 0, ADODB.SearchDirectionEnum.adSearchForward, "");
                                excelRange.AddComment(Environment.UserName + ":  " + _dataFactory.GetTaggedPartPrevValue(rs.Fields["POSITION"].Value.ToString(), rr.Title));
                            }
                        }
                    }
                }

                if (nRowCounter++ % 500 == 0)
                {
                    progress.ProgressComplete = ((double)nRowCounter / (double)rs.RecordCount) * (double)100;
                    Notify();
                }
            }

            rs.Close();

            excelWorksheet.Columns.AutoFit();
            progress.Message = "Done Exporting to Excel";
            Notify();
            _dataFactory.RestoreDatabase();
        }
+3
6

ODBC?

''http://www.ch-werner.de/sqliteodbc/

dbName = "c:\docs\test"
scn = "DRIVER=SQLite3 ODBC Driver;Database=" & dbName _
& ";LongNames=0;Timeout=1000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;"

Set cn = CreateObject("ADODB.Connection")
cn.Open scn

Set rs = CreateObject("ADODB.Recordset")
rs.Open "select * from test", cn

Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs

, Excel HTML .

+2

Excel XML . , , XML Excel. Excel XML.

Update: ( ), excel, excellibrary, . Excel (.XLS .XLSX) # .

+2

Excel ADO DAO , CopyFromRecordset.

:

    Sheets("Sheet1").Range("A1").CopyFromRecordset rst
+2

Excel SQLite. Excel " ". OLE DB, , - . alt text

, , - , .

, :

  • Excel
  • , Excel,

, , , , - ?

+1

, , .

I would consider this chain of events:

  • query the SQLite database for your dataset.
  • Move data from ADO.NET objects to POCO objects. Stop using DataTables / Rows.
  • use For Eachto paste into excel.
0
source

Source: https://habr.com/ru/post/1765630/


All Articles