Solved

export to excel

Posted on 2007-04-04
6
584 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

815 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