Hi
I'd like to use C# to open an existing Excel workbook and save each
worksheet it contains into a new Excel file. The name of each new
Excel file should be the name of the worksheet copied from the existing
file.
So, as an example:
Names.xls
---------
Fred becoming Fred.xls, Mary.xls, Bob.xls, Sue.xls
Mary
Bob
Sue
If anyone could give me any pointers as to how to do this, I'd be very
grateful.
Thanks
Adrian 1 10745
I've now come to a solution for this, which I'll show here in case
anyone else is pondering the same problem:
private void button1_Click(o bject sender, System.EventArg s e)
{
//Start Excel and get Application object.
Excel.Applicati on oXL = new Excel.Applicati on();
Excel._Workbook oWB;
Excel._Workbook oWBNew;
Excel._Workshee t oSheet;
Excel.Range oRng;
Excel.Worksheet worksheet;
Excel.Sheets sheets;
string filename = "";
try
{
oXL.Visible = true;
//Turn off Excel message alerts
oXL.DisplayAler ts = false;
//Get the relevant workbook from the file entry textbox
oWB = (Excel._Workboo k)oXL.Workbooks .Open(txtFilePa th.Text, 0,
true, 5, "", "", true, Excel.XlPlatfor m.xlWindows, "\t", false, false,
0, true);
// get the collection of sheets in the workbook
sheets = oWB.Worksheets;
//Copy the names to an array
string[] strNames = new string[sheets.Count];
for (int p = 0; p < sheets.Count; p++)
{
worksheet = (Excel.Workshee t)sheets.get_It em(p+1);
strNames[p] = worksheet.Name;
}
oWB.Close(oMiss ing, oMissing, oMissing);
for (int i = 0; i < strNames.Length ; i++)
{
//Reopen the workbook
oWB = (Excel._Workboo k)oXL.Workbooks .Open(txtFilePa th.Text, 0,
true, 5, "", "", true, Excel.XlPlatfor m.xlWindows, "\t", false, false,
0, true);
sheets = oWB.Worksheets;
for (int x = 0; x < strNames.Length ; x++)
{
//Delete the worksheet
unless it's the one we're interested in
if (!(strNames[i].Equals(strName s[x])))
{
worksheet = (Excel.Workshee t)sheets.get_It em(strNames[x]);
worksheet.Delet e();
}
}
//Save the worksheet as the new
name
oWB.SaveAs(("c: \\ConvertedExce lFiles\\" +
strNames[i]),oMissing,oMis sing,oMissing,o Missing,oMissin g,Excel.XlSaveA sAccessMode.xlN oChange,oMissin g,oMissing,oMis sing,oMissing);
//Close the original workbook
oWB.Close(oMiss ing, oMissing,
oMissing);
}
}
catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage, theException.Me ssage );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = String.Concat( errorMessage, theException.So urce );
MessageBox.Show ( errorMessage, "Error" );
}
finally
{
//Re-enable Excel's alerts
oXL.DisplayAler ts = true;
//Exit the Excel application
oXL.Quit();
}
}
Basically, I'm opening the original workbook each time, deleting all
but the appropriate worksheet and then resaving it as the name of the
worksheet.
This works a treat, but someone may have a more elegant solution?
Adrian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: I Decker |
last post by:
Hi all,
Hope this is the right group.
I am writing a program in c# to open create an excel document, enter
some data, save it and then email it as an attachment. I have
successfully created an excel document which the user can see (at this
stage of development) and passed some data to it. I then used the
savas method to save the file. Again this seems to work as the file is
created. However once I close the excel file and try and...
|
by: dgoel |
last post by:
Hi,
I Have a text file & I want to open it in excel sheet ( withou
importing). I have written code for it, but it is not opening exce
sheet. It opens the text file, but does not create a excel sheet
populate it. I do not know what the problem is, can someone please hel
out with this one,
Here is my code:
StreamReader dataFileReader = null;
FileInfo file = null;
Excel.Application ExcelObj = new Excel.Application();
|
by: pmud |
last post by:
Hi,
I need to use an Excel Sheet in ASP.NET application so that the users can
enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever
the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a
button.
Is this possible? & what is the BEST APPROACH for doing this? & also if any
links are there do tell those to me too coz I have no idea how to go about
doing it.
|
by: Vanajakshi Pidatala |
last post by:
Hello,
I am trying to show data in dataset in to an excel
sheet(ASP.NET/VB.NET). I AM DOING THIS ON SERVER(IIS SERVER) WHICH HAS
EXCEL VERSION 9. I COULD ADD REFERENCE TO EXCEL VERSION 9 AND OFFICE
VERSION 10 IN MY PROJECT. I DO NOT HAVE EXC EL VERSION 10 TO WHICH I
CAN ADD REFERENCE (WHICH I THINK IS THE PROBLEM). i AM GETTING THE
ERROR ' REFERENCE TO NULL OBJECT AT THE CODE line
Dim excelBook As Excel.Workbook =...
|
by: |
last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It
works perfectly on my machine, but it fails on my customers' PCs that have
identical versions of Win XP (SP1) and Excel (SP1) installed.
The error is:
System.Runtime.InteropServices.COMException(0x800A03EC): Exception from
HRESULT: 0x800A03EC.
at Microsoft.Office.Interop.Excel._Worksheet.Paste(Object Destination,
Object Link)
at...
| |
by: Peter Stojkovic |
last post by:
I am importing data from an EXCEL XLS-File via OLEDB-Provider
The command is SELECT * from
Everything works fine if the first sheet is named sheet1
But sometimes the name is not known by my application and the import failes.
|
by: RAGHAVENDRAS |
last post by:
Hi,
I am writing a script which should update an excel sheet daily. So how do I determine which is the last row which contains data and then write the latest data into the sheet.
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel;
$datetime=localtime();
|
by: Taxman |
last post by:
Windows XP, MS Office Excel 2003
If the tasks, I’m trying accomplish have been addressed previously (separately or in combination). Please, provide the links or keyword search to find them. I’ve been searching for code for each part of the task separately and trying to piece together multiple macros, that do something similar, to what I’m trying to accomplish in my over all task, but I’m not having a lot of luck. So, here’s the entire task,...
|
by: TG |
last post by:
hi!
I am trying to create a sql server table from an excel sheet.
Here is the code I have:
'This procedure the xlsx file and dumps it to a table in SQL Server
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |