Importing from XML to Excel - need to know how to append to existing Excel file

Hello,
I have several XML files that I will need to import into one Excel spreadsheet. I have been able to figure out how to import an XML file to an Excel file but when I import a second XML file, the contents of the second XML overwrite the Excel spreadsheet.
I am looking for an option to append. Thanks in advance
private void XmlToExcel(string xmlFilePath, string excelFilePath)
object misValue = System.Reflection.Missing.Value;
DataSet Trans_ds = new System.Data.DataSet();
Trans_ds.ReadXml(xmlFilePath);
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
System.Data.DataTable table = Trans_ds.Tables[0];
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
Range excelRange = (Excel.Range)worksheet.get_Range("A" + worksheet.Rows.Count, Type.Missing);
int lastRow = excelRange.get_End(XlDirection.xlUp).Row;
int ColumnIndex = 0;
int rowIndex = lastRow++;
foreach (System.Data.DataColumn col in table.Columns)
ColumnIndex++;
excel.Cells[lastRow, ColumnIndex] = col.ColumnName;
foreach (DataRow row in table.Rows)
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
ColumnIndex++;
excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName];
worksheet.StandardWidth = 20.0;
worksheet.SaveAs(excelFilePath, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
excel.Quit();
releaseObject(worksheet);
releaseObject(excel);

See if this helps.  I opened the old excel file and added the new rows into the existing workbook.
private void XmlToExcel(string xmlFilePath, string excelFilePath, Boolean append)
object misValue = System.Reflection.Missing.Value;
DataSet Trans_ds = new System.Data.DataSet();
Trans_ds.ReadXml(xmlFilePath);
Excel.Application excel = new Excel.Application();
Excel.Workbook Bk = excel.Workbooks.Add(true);
System.Data.DataTable table = Trans_ds.Tables[0];
Excel.Worksheet worksheet = (Excel.Worksheet)excel.Worksheets[1];
Excel.Range excelRange = (Excel.Range)worksheet.get_Range("A" + worksheet.Rows.Count, Type.Missing);
int lastRow = excelRange.get_End(Excel.XlDirection.xlUp).Row;
int ColumnIndex = 0;
int rowIndex = lastRow++;
foreach (System.Data.DataColumn col in table.Columns)
ColumnIndex++;
excel.Cells[lastRow, ColumnIndex] = col.ColumnName;
foreach (DataRow row in table.Rows)
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
ColumnIndex++;
excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName];
worksheet.StandardWidth = 20.0;
if (append == false)
worksheet.SaveAs(excelFilePath, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
else
//open old workbook
Excel.Workbook oldworkbook = (Excel.Workbook)excel.Workbooks._Open(
excelFilePath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
//get first sheet in workbook
Excel.Worksheet oldxlSht = (Excel.Worksheet)oldworkbook.Worksheets[1];
//get column A of old worksheet to find last row in sheet
Excel.Range xlRange = (Excel.Range)oldxlSht.get_Range(
"A" + oldxlSht.Rows.Count, Type.Missing);
int LastRow = xlRange.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row;
int newrow = LastRow + 1;
//get last row of new worksheet
xlRange = (Excel.Range)worksheet.get_Range(
"A" + oldxlSht.Rows.Count, Type.Missing);
LastRow = xlRange.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row;
// copy rows of new worksheet to old worksheet
xlRange = (Excel.Range)worksheet.Rows["1:" + LastRow, Type.Missing];
xlRange.Copy(oldxlSht.Rows[newrow, Type.Missing]);
oldworkbook.Save();
excel.Quit();
releaseObject(oldxlSht);
releaseObject(worksheet);
releaseObject(excel);
jdweng

Similar Messages

  • I have a copy of my profile from a crashed harddrive need to know how to put bookmarks back into new harddrive

    ''locking - duplicate - https://support.mozilla.com/en-US/questions/782357''
    my old laptop crashed and a copy of the profile from firefox was copied from the crashed hard drive. I need to know how to retrieve my bookmarks from this profile file.

    Hopefully he didn't copy the Firefox program folder, but the [http://kb.mozillazine.org/Profile_folder_-_Firefox Firefox Profile Folder] in the hidden %AppData% location.
    See:
    * [[Recovering important data from an old profile]]

  • Deleted a photo from lg g2 and need to know how to retrieve it

    i deleted a very important photo from my lg g2 and i dont know how to retrieve it.

        angelshaven,
    Let's see where we can find that important photo.  When did you delete the photo? Have you set up the Verizon Cloud application before? If you've backed your photos up through the Verizon Cloud, then we'd definitely be able to retrieve it.  http://vz.to/1rVG0ux
    TrevorC_VZW
    Follow us on Twitter @VZWSupport

  • How to Append an existing Excel file?

    I am using the jexcel api to write to a new excel file. Each time i run my program, it creates a new Excel File by the name i give it. my code to create the excelFile is as below:-
    File excelFile=new File(workDir+"\\"+fileName+".xls");
    WritableWorkbook workbook = Workbook.createWorkbook(excelFile);
    After this code i write some data to a worksheet in the above mentioned excel file. But my requirement is that I want to create the excel if it does not exist, and if it exists, then the data should be appended to the worksheet in the existing excel file. At present it creates a new excel file and does not append it.
    How can it be done? does anyone have any suggestions as to how can i tackle this problem? please suggest something.
    Ankit

    There is no ready-to-run example, as you are searching for, available.
    This has several reasons:
    - It depends on the version of Excel. Examples for Excel 2007 might not work with Excel 2010 for instance.
    - No one just wants to open Excel without doing anything with it.
    The second point is something, i want to ask you:
    Why do you want to open an existing Excel file at all? What is it, what you want to do after opening it?
    If you have LV/C/C#/VB examples doing what you are looking for, why do you want to have it in TestStand directly, so NOT using existing code (saving time and effort) and basically re-inventing the wheel?
    ActiveX is an interface for remote controlling and embedding applications with/in other applications on Windows systems. It is a PROGRAMMING interface.
    So naturally, examples and tutorials exist for programming languages. Those examples are easily converted to TestStand sequences with a basic knowledge of the programming language and TestStand. Providing links for such tutorials/examples is a valid way to give support to fellow developers because we expect the fellow to have that basic knowledge. 
    If that knowledge is not present, i highly recommend to fill that gap.
    just my 2 cents,
    Norbert
    CEO: What exactly is stopping us from doing this?
    Expert: Geometry
    Marketing Manager: Just ignore it.

  • Dears I buy I phone from USA and I live in Iraq when I insert the SIM card it was not work becuase the I phone need actavetion from apple so I need to know how I can do that

    if you can support me to solve this problem of my I phone

    If you bought the new phone from an authorized dealer you should be able to turn it on and see the setup assistant that guides you through the process of setup and activation. What happens when you turn the phone on?
    Or did you buy a used phone from someone? What exactly do you see on the phone when you turn it on? What messages are displayed?

  • Hello I need to know how to export a frame as TIF file

    Hello I need to know how to export TIF or JPEG Files from the Timeline. Thanks for your assistance.

    Place the playhead on the frame you wish to export.
    Go to the menu File > Export > Using Quicktime Conversion . . .
    At the bottom of the pane that open, choose Format: Still Image and click the Options Button
    In the pane that opens, choose either Tif of Jpeg and click OK
    Navigate to where you want the image stored, change the name if you want, and click Save.
    MtD

  • HT4489 please i need to know how can i import my contacts from the pc saved as vcard i tried to import but it gives me an error

    please i need to know how can i import my contacts from the pc saved as vcard? i tried to import a vcard contact but it gives me an error

    Time Machine is a back up you have to set up to back up before there is a problem like a hard drive failure.
    It is possible to recover files from a failed drive, but it is an expensive proposition.
    There is a faint hope that an app like Disk Warrior or TechTool Pro might repair the drive. You could try making an appointment at an Apple store and ask a technician there to take a look... the diagnosis will not cost you.
    http://www.apple.com/retail/geniusbar/

  • Please help...I was downloading my pics from the SD card. I then I highlighted 5 pics and selected erase, once I selected erase 20 other pics was also erase. I need to know how can those erase pics be recovered? Please Help!

    Please help...I was downloading my pics from the SD card. I then I highlighted 5 pics and selected erase, once I selected erase 20 other pics was also erase. I need to know how can those erase pics be recovered? Please Help!

    You will need data recovery software. See this post:
                                                                                       Re: I accidentally lost 3 weeks of photos, how do I recover them?                                            
    And please send feedback to Apple: This is a bug. Only the imported photos should have been deleted, not all photos on the card. You can use this form: Apple - iPhoto - Feedback

  • I need to know how best to import .mod files into elements 7

    I need to know how best to import .mod files into elements 7

    lornamarieslade
    Are you working with .mod standard or widescreen?
    If .mod standard, less problematic. The file just might import without issue. Worst case would probably be changing the file extension from .mod to .mpg just prior to import.
    Now .mod widescreen is another story that revolves around maintaining the 16:9 flag that scretches the video for display after encoding. Some have said that the 16:9 flag is lost in the transfer to computer; some say certain editors such as Premiere Elements just cannot read/recognize the flag. Whatever the case this is what you should consider in your Premiere Elements 7 Windows.
    a. If you need to rename the file extension from .mod to .mpg, do so prior to import.
    b. Import the .mod or .mpg counterpart into your Premiere Elements 7 project using the project preset NTSC or PAL DV Widescreen (NTSC or PAL depends on your location of interest). You will probably see that the video does not fill the space in the Edit Mode Monitor (you will probably see standard 4:3 looking image rather than widescreen 16:9 looking image).
    c. Right click the file in the Project media area, select Interpret Footage, and, in the Interpret Footage Dialog/Pixel Aspect Ratio section, dot the Conform To: and change the setting to
    D1/DV NTSC Widescreen 16:9 (1.2)
    or
    D1/DV PAL Widescreen 16:9 (1.422)
    depending on your location of interest.
    A while back there used to be a program called SD Copy that did the job of file name change and adding the 16:9 flag prior to import of the .mod widescreen into Premiere Elements. But, I have lost track of the SD Copy and suspect that is it no longer available. I have dealt with this issue many times at a forum that I formerly frequent. Most have reported back that this workaround worked for them. It is worth a try. I am looking forward to your results.
    If you do opt to upgrade to Premiere Elements 11 Windows (please go tryout before purchase), I am eager to learn how your .mod widescreen is handled by version 11 in your computer environment.
    Thanks.
    ATR
    Add On...I just came across this download for SDCopy. It does not give the appearance of the original the link to which is broken. I have not downloaded and used the following since I do not have any .mod widescreen. So, no guarantees. Please let us know the outcome if you try it if Interpret Footage does not work for you.
    http://paulmcgrath.net/download.php?view.8

  • HT1918 I need to know how to delete my credit card from account because I know longer have a credit.  I just want to be able to up date my Apps but I can't now because there is a credit card on my account what do I need to delete it.

    I need to know how to delete my credit card from account because I know longer have a credit.  I just want to be able to up date my Apps but I can't now because there is a credit card on my account what do I need to delete it.

    I need to know how to delete my credit card from account because I know longer have a credit.  I just want to be able to up date my Apps but I can't now because there is a credit card on my account what do I need to delete it. Also my credit has expired so i need to delete it.

  • I have a 3g iphone and I need to know how to delete old photos from my phone!! I need the space but there is no way to delete the pictures!! Help!!!

    I have a 3g iPhone and I need to know how to delete old photos. Need to free up space and there is no clear way to delete these photos!! Can someone please let me know how to get rid of these pictures!!!! Help!!!

    There are a number of ways to delete pictures from the Camera Roll.  One is to open Camera Roll in the thumbnail view, tap the arrow button (top right), tap all the pictures you want to delete, tap the Delete button ant the bottom.  Photo Library pictures are deleted by unchecking Sync Photos on the Photos tab of your iTunes sync settings, followed by syncing your phone.

  • I just got my first Apple product (ipod touch) and am needing to know how to transfer music from another itunes list. Can music that is currently on another ipod and/or computer be transferred to my new touch and my new itunes account? Thank you!

      
    Recently purchased my first Apple product (ipod touch) and am needing to know how to transfer music from other ipods and other itunes accounts. Can this be done? I have plugged in another ipod into my new itunes account and it will not let me transfer the music that it contains into my new account. Any/all help is much appreciated. Thank you!

    I have no music in my itunes account yet. I am trying to transfer music from friends' accounts and their ipods/playlists.

  • I bought my I phone 4 from a pawn shop and I need to know how to get rid of the old owners apple ID and use mine

    I need to know how can I get rid of the old owners apple ID so that I can use mine to be able to down load stuff from I tunes and get rid of his / hers apps
    Please can someone help me I bought this phone from a pawnshop.

    Restore the iphone.
    http://support.apple.com/kb/ht1414

  • I had purchased an audio book from itunes on my iPad and when I synced my iPad with my computer, my audiobook disappeared. I have already purchased this audiobook, so now I just need to know how to get it back because it is not on iPad anymore.

    I had purchased an audio book from itunes on my iPad and when I synced my iPad with my computer, my audiobook disappeared. I have already purchased this audiobook, so now I just need to know how to get it back because it is not on iPad anymore. I don't think the audiobook was backed up to icloud but I have the receipt to prove I purchased it. Is there anyway I can get it back?

    "If I purchase an audio book in iTunes on my iPad why can't I listen to it on my Mac? "
    You can.  Why do you think you cannot?
    If you purchased it on your ipad, then you would have to transfer it to your computer. File>Devices>Transfer Purchases.
    You can then listen to it on your computer.

  • My i phone 4s was stolen today. I need to know how to retrieve my photos from i cloud from i cloud, not my device because I don't have it!

    My phone was stolen. All of my pictures that are important to me are on it on i cloud or at least should be. I do not have my phone because some jerk stole it, therefore need to know how to retrieve my photos that were streamed. Someone please help me. When I go onto i cloud there is no tab or application for me to click on related to photos. All I have the option of going to is: calendar, contacts, reminders and that's about it. Where are my pictures?!

    Hi Elizabeth,
    You photos are only stored in the iCloud PhotoStream, or as an iCloud backup, which you can back up to a new device that has been signed on to the same iCloud account. There is no direct way to see photos that are in iCloud.
    You would have needed to have backed them up to either iPhoto on your Mac, or to the Photos file on a PC.
    GB

Maybe you are looking for

  • Xl Reporter Timeout Setting

    Hi All We have the following message when trying to generate a large report in XL Reporter.  An error occurred while Executing report! Decription: Rows 2358, Column 244 Timeout expired This is a very large report and it times out before it completes.

  • ITunes 32 bit on 64 bit Windows 7?

    Why is it that I cannot install iTunes 32bit on a 64 bit Windows 7 system? I've never had a 32bit program reject my attempt to install it on a 64 bit system before; what's different?

  • Accessibility Full Check in Acrobat X Pro vs Acrobat XI Pro

    My colleague and I just ran an Accessibility Full Check on a PDF document first using Acrobat X Pro, and then again on the exact same document using Acrobat XI Pro. The check using X found no problems, but the check using XI found numerous problems.

  • New version of iTunes has bug i.e. downloaded mp3 has very different title than original

    When I import the downloaded mp3 the title is very different and offensive words. What is up with this? thnx

  • Want to learn SAP BW

    I saw that question befor but it was not answered. I am going to work in a company which is using BW. So want to leran it befor. Which installation must I do to start learning SAP BW. Thankful for any Help Engin Turhan