Solved

export to excel

Posted on 2007-04-04
6
581 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now