List of lists excel export

Strategically, you are doing it correctly. As Joe says, it is massively faster to execute cell value assignments by passing an entire array of values in one shot rather than by looping through the cells one by one.

Excel is COM based and so operates with Excel via the .NET interop. The interop is ignorant of generics, unfortunately, so you cannot pass it a List<T> or the like. A two dimensional array really is the only way to go.

That said, there are a few ways to clean up your code to make it a bit more manageable. Here are some thoughts:

(1) If you are using .NET 3.0, you can use LINQ to shorten your code from:

int numberOfColumns = int.MinValue;

foreach (List<object> outputColumns in outputRows)
{
        if (numberOfColumns < outputColumns.Count)
        { numberOfColumns = outputColumns.Count; }
}

to a single line:

int numberOfColumns = outputRows.Max(list => list.Count);

(2) Don’t use the _Worksheet or _Workbook interfaces. Make use of Worksheet or Workbook instead. See here for a discussion: Excel interop: _Worksheet or Worksheet?.

(3) Consider making use of the Range.Resize method, which comes through as Range.get_Resize in C#. This is a toss-up though — I actually like the way you are setting your range size. But it’s something that I thought that you might want to know about. For example, your line here:

Excel.Range oRng = oSheet.get_Range("A1", oSheet.Cells[numberOfRows,numberOfColumns]);

Could be changed to:

Excel.Range oRng = 
    oSheet.get_Range("A1", Type.Missing)
        .get_Resize(numberOfRows, numberOfColumns);

(4) You do not have to set the Application.UserControl to true. Making Excel visible to the user is enough. The UserControl property is not doing what you think it does. (See the help files here) If you want to control whether the user can control Excel or not, you should utilze Worksheet protection, or you could set Application.Interactive = false if you want to lock out your users. (Rarely a good idea.) But if you want to allow the user to use Excel, then simply making it visible is enough.

Overall, with these in mind, I think that your code could look something like this:

object oOpt = System.Reflection.Missing.Value; //for optional arguments
Excel.Application oXL = new Excel.Application();
Excel.Workbooks oWBs = oXL.Workbooks;
Excel.Workbook oWB = oWBs.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet oSheet = (Excel.Worksheet)oWB.ActiveSheet;

//outputRows is a List<List<object>>
int numberOfRows = outputRows.Count;
int numberOfColumns = outputRows.Max(list => list.Count);

Excel.Range oRng = 
    oSheet.get_Range("A1", oOpt)
        .get_Resize(numberOfRows, numberOfColumns);

object[,] outputArray = new object[numberOfRows, numberOfColumns];

for (int row = 0; row < numberOfRows; row++)
{
    for (int col = 0; col < outputRows[row].Count; col++)
    {
        outputArray[row, col] = outputRows[row][col];
    }
}

oRng.set_Value(oOpt, outputArray);

oXL.Visible = true;

Hope this helps…

Mike

You can export to either:


  • Excel workbook
        The data has a one-way connection to the original list. Periodically refreshing the workbook pulls in changes that have been made in the list.

  • CSV (comma-separated values)file    It can be opened in Notepad or Excel. The data is not connected to the original list. (On macOS, exporting to CSV is the only available option.)

    Important: 

    • The maximum number of rows you can export to a CSV file is 30,000.

    • You might have noticed ‘Export to CSV’ option enabled on document libraries for a brief period. The option was enabled unintentionally so we had to disable it. We would love to bring the capability to document libraries, and we are working on it but do not have any time estimate that can be shared.

Export to an Excel workbook

  1. From the command bar of the list, select Export > Export to Excel.

    The Export options for a SharePoint list.

    Important: The Export options are available only when list items are not selected.

    Depending on your browser, you can save and open, or open the file directly.

    Note: If you save the file, the default file name for the first copy is query.iqy, and it is placed in the default download folder for your browser. To change the name and location, use your browser’s Save as.

  2. If needed, select OK > Open after the file download begins.

  3. If prompted and you trust the SharePoint site, in the Excel security page, select Enable.

  4. In the Excel Import Data dialog box, select the How you want to view this data and Where do you want to put the data options.

    Import data dialog box from Excel 2016

  5. When done, select OK. The list should appear in Excel.

    Excel spreadsheet with imported list and Refresh All button highlighted.

Excel creates an Excel table with a one-way data connection based on a web query file. To bring a fresh copy of the SharePoint list to Excel, select Refresh All on the Data tab. Changes made to Excel table will not be sent to the SharePoint list.

If your SharePoint list contains folders, the folder structure does not appear in the resulting Excel table. However, the Item Type and Path columns are added to the Excel table so you can filter and sort the data, based on its type and location or subfolder of the list.

Export to a CSV file

When exporting to CSV, you can export a maximum of 30,000 rows of data.

  1. Open the list whose data you want to export.

  2. From the command bar above the list, select ExportExport to CSV as the type of data output.

    (On macOS, from the command bar above the list, simply select Export to CSV.) 

    Note: The Export option is available only when list items are not selected.

  3. Select Open to open the file in Excel. Then you can save the file as an Excel workbook, which allows you to add format the data as you like.

    Alternatively, select Save as to save the file in .csv format on your computer. 

  1. From the command bar of the SharePoint list, select Export to Excel  Lists Export To Excel Icon.

    Important: 
    Export to Excel is available only when list items are not selected.

    The classic SharePoint experience shows a ribbon above the list, not a command bar. Select the List tab on the ribbon, and then select Export to Excel.

    SharePoint Export to Excel button on ribbon highlighted

    Depending on your browser, you can save and open, or open the file directly.

    Note: If you save the file, the default file name for the first copy is query.iqy, and it is placed in the default download folder for your browser. To change the name and location, use your browser’s Save as.

  2. If needed, select OK > Open after the file download begins.

  3. If prompted and you trust the SharePoint site, in the Excel security page, select Enable.

  4. In the Excel Import Data dialog box, select the How you want to view this data and Where do you want to put the data options.

    Import data dialog box from Excel 2016

  5. When done, select OK. The list should appear in Excel.

    Excel spreadsheet with imported list and Refresh All button highlighted.

Excel creates an Excel table with a one-way data connection based on a web query file. To bring a fresh copy of the SharePoint list to Excel, select Refresh All on the Data tab. Changes made to the Excel table will not be sent to the SharePoint list.

If your SharePoint list contains folders, the folder structure does not appear in the resulting Excel table. However, the Item Type and Path columns are added to the Excel table so you can filter and sort the data, based on its type and location or subfolder of the list.

  • #2

What is does ‘list of lists’ mean?

And ‘unbounded’?:eek:

  • #3

Hi, sorry .. the tags I put in to show the xml example didn’t show up. So instead of using «<» and «>» I’ll use «{» and «}».

«List of lists» is the thing I am trying to figure out … :- ???
«Unbounded» is just the term they use when you set the «max value» of a tag to «unbounded.» It just means that the data can repeat itself and is not limited to just 1 (or any number) of entries.

So …. let’s try this again.

I am trying to import an XML file to excel, be able to edit it in excel, then export it from excel. I’m using excel 2007.
On the developer tab, they have this import and export XML function.
I import the table but when I go to export it, they say they can’t because of the error «List of lists.»

I researched the error and it is as I described above, that the problem is that, «One list of items contains a second list of items.»

So I’m just looking for confirmation that my interpretation of that is the same as others. What I take that to mean is that if I have the following XML:

{Organization}
{Company}
{Name}Company1{/Name}
{Employee}Emily{/Employee}
{Employee}Joe{/Employee}
{Employee}Sue{/Employee}
{/Company}
{Company}
{Name}Company2{/Name}
{Employee}John{/Employee}
{Employee}George{/Employee}
{Employee}Jen{/Employee}
{/Company}
{/Organization}

… that it could never be exported because I have a «list of lists.» So the first «list» is that there are multiple companies. And then within that company I have another list, the list of employees.

That would be really crappy because this is a very basic XML file and if excel can’t export a «list of lists» then it is by no means a good software to use to edit XML files.

So I hope my interpretation of the problem is wrong. I did some initial testing and indeed, if I remove the «Employee» tag it can export.

  • #4

Where is all those strings ?

  • #5

Can you clarify what you mean? The strings are in an XML file with a *.xml extension that I import into excel. I can get an example of an XML and the XSD file I use if needed.

  • #6

try

Rich (BB code):

Sub test()
Dim fn As String, temp As String
fn = "c:test.xml"  '<- alter here (file path)
temp = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
temp = Replace(temp, vbCrLf, Chr(12))
With CreateObject("VBScript.RegExp")
    .Pattern = Chr(12) & "*< Employee ><EMPLOYEE>.+< /Employee ></EMPLOYEE>" & Chr(12) & "*"  '<- delete the sapce
    temp = .Replace(temp, "")
End With
Open Replace(fn, "xml","Revised.xml") For Output As #1
    Print #1, Replace(temp, Chr(12), vbCrLf)
Close #1
End Sub

Last edited: Feb 14, 2009

  • #7

This worked. Thank you jindon.

  • Remove From My Forums
  • Question

  • Hi,

    I am trying to convert a complext spreadsheet from excel to a XML. Also beacuse of the requirements of the destination application requirement i need to follow the specific schema.

    I was able to map all the elements in excel spreadsheet and export the spreadsheet in the required xml format. But the problem starts when there is a ‘List of List’ in the XML data. and the data is not normalized. when i am truing to map the child of child , i am not able to retain the relationships.

    I know with .net  it is possible to build a application from scracch to do it. but with the time and application contraint, is there a way to handle this mapping of excel data?

    Thanks

    Kedar Adavadkar

so I know we can export a single list to an Excel Spreadsheet, but is there a way export multiple lists on that same spreadsheet?

asked Nov 25, 2015 at 15:43

John Kirchoff's user avatar

there is no OOT functionality which support this but here is workaround, which may solve you problem.

  1. Go to SharePoint List A and then click on List tab on Ribbon control

  2. Then click on ‘Export to Excel’ button on ribbon control

  3. Save the file with meaningful name, for example: owssvrA.
  4. Repeat the steps for 1 to 3 for List B and save file with another name:owssvrB.

  5. Now we have ‘MS Excel Web Query File’ files for both lists.

  6. Open new excel application.
  7. Go to Sheet1 and click on any cell.
  8. Click on Data in ribbon

  9. Click on Existing Data Connection

  10. Click on ‘Browse for More’ and go to the location where ‘’MS Excel Web Query File’ files are saved.

  11. Select List A’s Query File and click Open.

  12. On Import Data pop up select ‘New Worksheet’, (if you know exact column numbers then choose existing worksheet and cell range).
  13. Now you have data of List in your Excel sheet.
  14. Rename sheet with name: “Sheet1” and you are done for list A.
  15. Now repeat same steps for List B and you have two lists data in two separate sheets. You can refresh data as well from the ribbon button. Isn’t it easy??

Source

answered Nov 25, 2015 at 16:16

Waqas Sarwar MVP's user avatar

Waqas Sarwar MVPWaqas Sarwar MVP

56.8k15 gold badges40 silver badges77 bronze badges

5

Once you have an Excel workbook open, any time you Export a list to Excel it will prompt you asking where to put it. So if you want it in the same sheet or the same workbook, you can just select that option.

enter image description here

answered Dec 2, 2015 at 20:29

Erin L's user avatar

Erin LErin L

4,0381 gold badge13 silver badges33 bronze badges

1

  • Open List A in MS-Access
  • Open List B in the same Access file
  • Create a Query to Join the 2 lists
  • Export or Link to one Excel file

answered Nov 25, 2015 at 16:22

Danny '365CSI' Engelman's user avatar

I have seen this so many times and also always wanted to know if there was a way. Craig Tarr wrote up a way on how to do it using front-end code. Basically the script structures unstructured data using REST API. Once you have the data you can export to excel or another DB. Completely configurable.

https://www.gtconsult.com/extracting-unstructured-data-that-should-be-structured/

answered Oct 19, 2016 at 7:27

Bradley Geldenhuys's user avatar

1

Понравилась статья? Поделить с друзьями:
  • List of items word
  • List of irregular verbs word
  • Link not updating in excel
  • Link not opening in word
  • List of images in word