Solved

export to excel

Posted on 2007-04-04
6
585 Views
Last Modified: 2013-12-16
I am importing from excel using the code below.  If a row fails my validation, I want to export those rows in question to an excel file with the same name adding _fail.  So myfile.xls will export to myfile_fail.xls.

I have seen comments on how to export to excel, but I wanted to just update this file and save it under a different name.  If they decide to make the header a certain font, or color, I would like my new file to be the exact same format with the successful rows removed.  I do not want to care about how the file is set up.

Here is the code for importing that I am using.

Function ReadDataFromExcel(ByVal excelfilename As String) As DataSet
        Dim ds As New DataSet
        Dim da As OleDbDataAdapter
        Dim conn As OleDbConnection
        Try

            conn = New OleDbConnection( _
                  "provider=Microsoft.Jet.OLEDB.4.0; " & _
                  "data source=" & excelfilename & "; " & _
                  "Extended Properties=Excel 8.0;")

            da = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn)

            conn.Open()

            da.Fill(ds)


        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try
      ReadDataFromExcel=ds
    End Function
0
Comment
Question by:jackjohnson44
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 18851381
I would not use OleDbConnection for this.

You should be using a COM reference.  That way, you have total control over Excel.  The reference is called "Microsoft Excel XX Object Library".  Where XX is the version that you have installed on your machine.

See this article.  It should get you going:

http://support.microsoft.com/kb/302084
0
 

Author Comment

by:jackjohnson44
ID: 18851532
Thanks, but I really don't want to have to specify columns because I don't know what they will be.  I pretty much want to edit the excel file I am working with and save it under another name.  Is this possible?
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 18853157
I would still use the COM object.

Anything you can do in Excel can be done via the COM object including getting the file name and saving to another file.

Also, "SELECT * FROM [Sheet1$]" would be the same as iterating over the UsedRange object.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:jackjohnson44
ID: 18853553
The link you posted does not show how to open a file, edit it and save it.  How would I open a file, delete rows 3 and 5 and save it under a different name?  I do not want to have to worry about font, spacing and headers.  I want an exact copy of my current file with a few lines missing.  Thanks
0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 18854254
0
 
LVL 22

Accepted Solution

by:
Bill-Hanson earned 500 total points
ID: 18857251
It's really easy.  Here's some code to get you going.  This example opens one file, removes row 3 and saves under a new filename.

using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;

       private void SimpleTestButton_Click(object sender, EventArgs e)
        {
            String strOldFilePath;
            String strNewFilePath;
            Excel.Application oXL;
            Excel.Workbook oWb;
            Excel.Worksheet oWs;
            object missing = Type.Missing;

            strOldFilePath = "C:\\Temp\\test1.xls";
            strNewFilePath = "C:\\Temp\\test2.xls";

            oXL = new Excel.Application();
            oXL.Visible = true;
            oWb = oXL.Workbooks.Open(strOldFilePath,
                missing, missing, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing);

            oWs = (Excel.Worksheet)oWb.Worksheets[1];

            Excel.Range row = oWs.get_Range("A3", missing);
            row.EntireRow.Delete(missing);

            oWb.SaveAs(strNewFilePath,
                Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
                false, false, Excel.XlSaveAsAccessMode.xlNoChange,
                missing, missing, missing, missing, missing);
        }

Also, here's the link to the entire Excel API reference:
http://msdn2.microsoft.com/en-us/library/aa272268(office.11).aspx
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question