How to open Excel FIles
I am a novice
When I open excel file I found some problems.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows.Forms;
using XLS=Microsoft.Office.Interop.Excel;
using WD=Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
namespace ExcelDataFillWord
public partial class Form1 : Form
public Form1()
InitializeComponent();
private void button1_Click(object sender, EventArgs e)
// Scheduler sched = new Scheduler();
//foreach (Task t in sched.Tasks)
// Console.WriteLine(t.ToString());
// foreach (Trigger tr in t.Triggers)
// Console.WriteLine(tr.ToString());
private void label1_Click(object sender, EventArgs e)
FileDialog.Filter = "excel文件|*.xls|Excel文件|*.xlsx";
FileDialog.Multiselect = false;
FileDialog.Title = "打开excel文件";
if (FileDialog.ShowDialog() == DialogResult.OK)
textBox1.Text = FileDialog.FileName;
object missing = System.Reflection.Missing.Value;
//object missing = Type.Missing;
CloseProcess();
//'Excel::Application excel =new Excel::ApplicationClass();
Microsoft.Office.Interop.Excel.Application excel= new Microsoft.Office.Interop.Excel.Application();
//Excel::Workbook Wk =(Excel::Workbook )Xls.Workbooks.Open(textBox1.Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing);
Microsoft.Office.Interop.Excel.Workbook workbook =(Microsoft.Office.Interop.Excel.Workbook) excel.Application.Workbooks.Open(textBox1.Text , missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//comboBox1.Items.Clear();
//foreach (Excel::Worksheet Sh in Wk.Worksheets)
// comboBox1.Items.Add((Sh.Name).ToString());
workbook.Close(missing, missing, missing);
excel.Quit();
private void CloseProcess()
System.Diagnostics.Process[] excelProcess = System.Diagnostics.Process.GetProcessesByName("EXCEL");//实例化进程对象
foreach (System.Diagnostics.Process p in excelProcess)
p.Kill();//关闭进程
System.Threading.Thread.Sleep(10);//使线程休眠10毫秒
problem is : can't change excel.applicaitonclass's Com object to interface
so I want to ask a question:
excel.application is a interface ,then interface variant = a class( it inherients this interface) object
but interface is a abstract object ,It has no construct function . why We use Excel.Applcation xsl=new Excel.Application();
Hello,
Perhaps this will help. Open an Excel file, select a sheet, range then set a value, save then close. The idea here is not to write or save but demo a few basic operations.
Also note 'as is' all memory is cleaned up without resorting to what many do, force the garbage collector. This is important if objects are not cleaned up it is very possible in extreme cases to bring a machine down so one should ensure all objects are cleaned
up which only comes from well written code be it C# or VB.NET
using System;
using System.Data;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace Example_C1
public partial class Form1 : Form
public Form1()
InitializeComponent();
public void OpenExcelExample(string FileName, string SheetName)
if (System.IO.File.Exists(FileName))
bool Proceed = false;
Excel.Application xlApp = null;
Excel.Workbooks xlWorkBooks = null;
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;
Excel.Sheets xlWorkSheets = null;
Excel.Range xlCells = null;
xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
xlWorkBooks = xlApp.Workbooks;
xlWorkBook = xlWorkBooks.Open(FileName);
xlApp.Visible = false;
xlWorkSheets = xlWorkBook.Sheets;
for (int x = 1; x <= xlWorkSheets.Count; x++)
xlWorkSheet = (Excel.Worksheet)xlWorkSheets.get_Item(x);
if (xlWorkSheet.Name == SheetName)
Proceed = true;
break;
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet);
xlWorkSheet = null;
if (Proceed)
Excel.Range xlRange1 = null;
xlRange1 = xlWorkSheet.get_Range("A1");
xlRange1.Value = "Hello";
Marshal.FinalReleaseComObject(xlRange1);
xlRange1 = null;
xlWorkSheet.SaveAs(FileName);
else
MessageBox.Show(SheetName + " not found.");
xlWorkBook.Close();
xlApp.UserControl = true;
xlApp.Quit();
ReleaseComObject(xlCells);
ReleaseComObject(xlWorkSheets);
ReleaseComObject(xlWorkSheet);
ReleaseComObject(xlWorkBook);
ReleaseComObject(xlWorkBooks);
ReleaseComObject(xlApp);
MessageBox.Show("Done");
else
MessageBox.Show("'" + FileName + "' not located. Try one of the write examples first.");
private void ReleaseComObject(object obj)
try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
catch (Exception)
obj = null;
private void button1_Click(object sender, EventArgs e)
OpenExcelExample(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WS1.xlsx"), "Sheet3");
private void releaseObject(object obj)
try
if (obj == null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
catch (Exception ex)
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
finally
GC.Collect();
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
Similar Messages
-
How to open excel files from email in macro enabled workbooks NOT 2003-2007 worksheets?
I have recently installed Office 365 and it was working great. I needed to open excel documents via email with the macro enabled option. Yesterday it flicked back to only giving me the option to open in 2003-2007 workbook so now I am having issues with the
macros. I am contracting and trying to get work out and this has now caused huge problems.
Am thinking of uninstalling and reinstalling. I did have older versions of ms office so maybe there is some residual content there to remove? I have tried to change the associations and protocols to open in excel macro enabled workbooks but these tick boxes
are greyed out and I do not know how to gain access to these. Have been hunting all through my computer system but do not know how to do this. Am no expert.
If someone could help I would really appreciate it otherwise will need to find some computer shop expert I suppose...urgently ha.
Thanks in advanceit's sound like excel 13 disable the macros, turn on it will fix your issue.
see here:
http://office.microsoft.com/en-us/excel-help/enable-or-disable-macros-in-office-files-HA010354316.aspx
ps: use the local admin to change the setting, the reason why the setting grey out.
KR -
How to list all currently open Excel files with ActiveX?
Hello all!
I would like to program with ActiveX a way to retrieve a list (array of strings) of the filenames of the currently opened Excel files in windows. However, I am fairly new to the ActiveX/Excel business in LV and after hours looking at the "millions" of objects in the property nodes I give up and hope some kind soul here can help me. I couldn't find any information in this forum regarding the same issue, although some were close
I am using LV 7.1 without the Report Generation Toolkit.
Thanks!
/GoranHi KC,
thanks for your reply. I have in fact been able to find the desired things in the ActiveX properties/methods for Excel in LabView. It took me some looong work going through the functions which I suspected to be the ones. Anyway, I post a little jpeg here of the code if someone ever needs the same functionality. What it does is that it checks how many Excel workbooks are open and controls if each of them matches the searched Excel filename, if one does, it is closed.
Enjoy
/Goran
Attachments:
List open excel sheets and close open ones.jpg 104 KB -
Problem in opening excel file in different window
hi
i want to open excel file in different window it open in same window properly on the bases of condition but when i try to open in different with window.open( )
it give error that file has already used or path not exist OR requisted site either unavailable
how can i rectifiedHi.
I am too facing the same problem. I am using Jakarta Poi to display my resultset in an excel sheet directly(and not into a jsp)
The browser opens a window showing Open Save Cancel. If i click and save the excel sheet and then open, it opens. But clicking on Open directly doesnt open the excel sheet. Rather it gives the same error
The file name or path name does not exist.
The file you are trying to open is being used by another progrtam . close the document in other program and try again
The name of the work book your tring to save is the same as name of another document that is readonly .try saving workbook with different name
Can somebody please help me fix this problem, since its an urgent requirement. Mail me at [email protected]
Thanks
Hitesh -
How to open aspx. files in OS 10.8?
is this the right place to ask how to open aspx. files in OS 10.8.2?
Unfortunately aspx is a generic extender - it is a report generated by a server running Windows .net - it isn't an extender that actually tells what kind of file it is or what kind of data it holds. That's something you either have to know before hand so you can force the proper program to open it (or change the aspx extender to its real extender) or you have to find out what the file is.
At work our system's report generator gives everything an aspx extender. When I generate a report I decide whether it should be a PDF, Excel file, etc. After downloading the file I change its extender appropriately. -
Hi All,
Does anyone know how to launch Excel file in HTML or jsp?
Wait...i know one method to open Excel fiel using
Window.open(blah...blah..)
In this case we can specify toolbar option..
But in my application i am loading excel file in frame.But frame doesnt have any option to set toolbar.....
Please let me know if you have soln for this....
Regards,
SuryaHi,
I can open excel file using window.open...but my requirement is forcing me to open it in frame....And i m able to open it properly in frame...but i want toolbar to come along with that frame...
according to folowing lin they say it is not possible have scenario like this...
http://support.microsoft.com/kb/157353/EN-US/
Please see whether you can help in this regard... -
Hi I lost my iphone 4 a week back, but I have the contacts back up. now I bought new phone.
so tell me how could I import all my contacts to new phone, or esle at lease how to open .VCF file in excel. or outlookYou should be syncing your contacts with an app on your computer or cloud service (iCloud, Gmail, Yahoo, etc), and not relying on a backup. If you haven't been doing this, start now and then restore your old backup. You will then be able to sync the new contacts back into the phone. However, you will lose all messages, etc newer thant the backup.
-
I cannot Open EXCEL files transferred from my old HP laptop.
I am unable to open EXCEL files transferred from my HP LapTop. How to resolve?
The forum for MBP:
MacBook Pro
As for Excel, checked microsoft office community?
Maybe if you try export first....?
Vista has its own ownership permission flags. Maybe if you modify the file or a copy first, and then copy. -
How to transfer excel files(on ftp server) into internal table?
hello,everyone
pls tell me how to transfer excel files those on a ftp server into internal table?
ps.i know the function 'ftp_server_to_r3',it can help to transfer flat file.Hi,
I believe you want to get the data from the FTP Server to R3.
I am also sending the code. Have a look and it would help you.
First get the Password and user name and the FTP Server Path where file is stored and FTP Server Host name
FUNCTION zfi_ftp_get.
*"*"Local Interface:
*" IMPORTING
*" VALUE(I_FILENAME) TYPE C
*" TABLES
*" T_BLOB STRUCTURE ZFI_TLM_LENGTH OPTIONAL " is a table type with a field called line of length 992
*" RETURN STRUCTURE BAPIRET2 OPTIONAL
DATA : i_password(30) TYPE c,
i_user(30) TYPE c,
i_host(30) TYPE c,
i_rfc_destination TYPE rfcdes-rfcdest,
i_length TYPE i,
i_folder_path(100) TYPE c.
DATA: lv_blob_length TYPE i.
DATA: lv_length TYPE i, "Password length
lv_key TYPE i VALUE 26101957,
lv_password(30) TYPE c,
lv_ftp_handle TYPE i,
lv_cmd(80) TYPE c.
DATA: BEGIN OF result OCCURS 0,
line(100) TYPE c,
END OF result.
TYPES: BEGIN OF ty_dummy,
line(392) TYPE c,
END OF ty_dummy.
DATA: lt_dummy TYPE TABLE OF ty_dummy,
ls_dummy LIKE LINE OF lt_dummy.
i_password = 'vnhdh'.
i_user = 'sdkgd'.
i_host = 'sbnksbg'.
i_rfc_destination = 'SAPFTP'.
i_length = '992'.
i_folder_path = '/hioj/hohjk/hh'.
lv_length = STRLEN( i_password ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = i_password
sourcelen = lv_length
key = lv_key
IMPORTING
destination = lv_password.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = i_user
password = lv_password
host = i_host
rfc_destination = i_rfc_destination
IMPORTING
handle = lv_ftp_handle
EXCEPTIONS
not_connected = 1
OTHERS = 2.
IF sy-subrc = 1.
return-type = 'E' .
return-message = 'FTP Connection not Successful'.
APPEND return.
ELSEIF sy-subrc = 2.
return-type = 'E' .
return-message = 'FTP Connection not Successful'.
APPEND return.
ELSEIF sy-subrc EQ 0.
return-type = 'S' .
return-message = 'FTP Connection Successful'.
APPEND return.
CONCATENATE 'cd' i_folder_path INTO lv_cmd SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = lv_ftp_handle
command = lv_cmd
TABLES
data = result
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc = 1.
return-type = 'E' .
return-message = 'Command Error Occured during open of FTP Folder'.
APPEND return.
ELSEIF sy-subrc = 2.
return-type = 'E' .
return-message = 'TCIP Error Occured during open of FTP Folder'.
APPEND return.
ELSE.
REFRESH t_blob.
lv_blob_length = 992.
TRANSLATE i_filename TO LOWER CASE.
CALL FUNCTION 'FTP_SERVER_TO_R3'
EXPORTING
handle = lv_ftp_handle
fname = i_filename
IMPORTING
blob_length = lv_blob_length
TABLES
blob = lt_dummy.
t_blob[] = lt_dummy[].
ENDIF.
ENDIF.
ENDFUNCTION.
Regards
Sajid
Edited by: shaik sajid on Nov 16, 2010 7:25 AM -
How to open the file in gmail. As is keep indicate is not support. Previously I can easily open the excel or words sheets that send to my email but After download the Free Trial for Adobe Pro( Convert PDF file to Microsoft words. )
More information would be helpful, such as the file format and what the error message is in more detail.
-
Is it possible to open Excel file in XAML Windows Store App?
Hi,
I want open excel file from local folder(not from Url) within the windows store app.
Could you please help me how to implement ?
Thanks,
Siva varri
Thread response from msdnHi,
If you want to open an excel file by a windows store app. There is no direct API in windows store app can handle excel file. You will need to find a third party component. But
if you want to launch a file into its default handler, please see
Launcher.LaunchFileAsync API. And you can refer to the link below:
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh779671.aspx
private async void Button_Click(object sender, RoutedEventArgs e)
StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file =
await storageFolder.GetFileAsync("56.xlsx");
if (file != null)
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
// File launched
else
// File launch failed
else
// Could not find file
Best Wishes!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a
href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey. -
Cannot open excel files in iOS4
As soon as I upgraded to iOS4, I have been unable to open excel files. Sometimes I get a can't open error, sometimes it crashes and goes back to the homescreen and with some excel files it works. A Genius at the store was able to open one after he re-saved the file on his mac but suggested this inconsistency was a bug that Apple would fix. I get a lot of excel files from many difference people for business and this is a real obstacle. Does anyone know how to fix or if Apple will. Thanks.
Many are experiencing this issue. We are an software development and Managed IT services shop and all of our clients are experiencing this same issue when trying to open an Excel file created by SSRS.
Follow this post: http://discussions.apple.com/thread.jspa?messageID=11843341 -
Is there a way to open Excell file from the server and display in the UI and save it back on to the
Hello there,
Is there a way to open Excell file from the server and display in the UI and save it back on to the server? (like showing xell file as a datagrid - add rows, columns etc.)Hi Mike,
Welcome you to the forum.
You may try:
SELECT * FROM MyDBNameHere.dbo.OUSR T0
Thanks,
Gordon -
How to upload Excel file in BI using function module in abap program
How to upload Excel file in BI using function module in abap program?
Hi Anuj,
To upload the file , you can try a standard program "RSEPSFTP" .
while you execute the program , a selection screen appears in which the inputs should be give as
RFC destination - The target server name
FTP command- PUT
local file - your file name
local directory - path of your local file
remote file - your target file name
remote directory - where it has to be stored
Hope this is useful for you
Thanks & regards
Anju -
I have some users in my company that can't open Excel files from any document libraries on SharePoint 2010. The receive the error message "An error occurred. Please try again." We don't currently use the Excel calculations services. When
a user opens an excel file they are prompted to either Read Only or Edit.
This affects less than 5 people in a large company. They have the Full Control permissions and have been able to open the files previously. There has been no changes made to the server either. I can't find anything that points to this in the
event viewer.
It doesn't matter if which computer that they try to open this up from either. All computers are Windows 7 Professional with IE 9 and Office 2010 x64.
I can't find anything that points specifically to this exact error. None of the excel files contain pivot tables. This one has me stumped.I would make sure no one has open on machine / locked on machine (Would assume you checked this, but wanted to add)
Also would have assumed you restarted the machines (:))
Make sure the file security did not change (check to ensure no one changed the sercuity of the excel)
Make sure the version of excel is the same (Changing from 2010 back to 2007 affects the file, etc.. on convert) that no one converted and saved thus causing error in file.
Can they open other excel files?
It sounds like the excel file it self has changed if you cannot see issue in SharePoint logs.
If it is all SharePoint excel files - try deleting one persons access and readding them to see if that helps.
SharePoint has issues ;)
Maybe you are looking for
-
ITunes 5 not opening. Need disk in \Device\Harddisk4\DR6?
I cannot get iTunes to run following download from the Apple website. At startup, I get a box with the following message: iTunes.exe - No Disk There is no disk in the drive. Please insert a disk into drive\Device\Harddisk4\DR6 When I click on the "Ca
-
Background image - can't remove it !!
I'm new to pages and a medium mac user. I have just started a brochure using the template in pages. However, it has an object or text box that is a hibiscus flower & is in the background - but I can't select it - so I can't remove it. I've looked at
-
I am buying a new Dell PC with Elements version 12 pre-installed. Then I have to move my Elements version 5 documents to the new computer. I need to know any precautions, suggestions, etc. that you may have.
-
"Inconsistent cursor cache. Out of range cursor" error on dual processor multi thread
I have a C++ with Pro*C++ COM object running on a Win2000 OS on a dual processor Middleware. The program is being called by a service in a multi threaded way. An oracle error like ORA-2103 and SQL-02103 will be generated after running the program. We
-
Yoga tablet webpage response problem
Hi, Our newish (9 months) yoga tablet links to the internet via our home wifi with no problem but when it accesses a website it soon tells me that 'this page has become unresponsive' and we cannot navigate the site further. It does this even after cl