Solved

export to excel

Posted on 2007-04-04
6
589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

628 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