I've been using this code to read an excel file and it works fine when i am trying to read saved excel files
string con = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;" + #"Extended Properties='Excel 8.0;HDR=Yes;'";
using(OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]",
connection);
using(OleDbDataReader dr = command.ExecuteReader())
{
while(dr.Read())
{
var row1Col0 = dr[0];
Console.WriteLine(row1Col0);
}
}
}
theres and application which opens an excel file which updates data every second and here is the pic of the app
here you can see the excel file being opened in the taskbar.when i used the process code to read the path of the excel file using this code i get no data as the file located in that path is empty
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName == "EXCEL")
{
Console.WriteLine(theprocess.ProcessName, theprocess.Id);
string fullPath = theprocess.MainModule.FileName;
//fullpath = C:\\Program Files (x86)\\Microsoft Office\\Office12\\EXCEL.EXE
}
}
the application might be using the instance of ms excel directly.is there any alternative step to read this excel file by the process id directly instead of the path?
Thanks in advance.
Processes are not meant to be used this way. You can start, stop and kill a Process, but there is no way to gain access to it's memory and read a file (actually, any stream of data) with it. I also don't think that there is a way to access the path of the opened file by the Process class.
However, using the Office Interop COM API, you can get the current file, including it's path with Application.ActiveWorkbook.FullName.
Be advised, that this solution works only if one instance of Excel is opened.
Microsoft.Office.Interop.Excel.Application MyExcelApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
string FullPath = MyExceAppl.ActiveWorkbook.FullName;
Related
I am trying to upload an excel(.xlsx) file in my application which has been created by the same application via open xml sdk. I am facing the exception 'External table not in expected format'. However if i manually open the file and save it and try again, it is uploaded without any errors.
Is there any way to programatically perform the task of opening the excel file and save ? I cannot ask my user/client to follow this workaround. Any leads would be helpful. Below is the code snippet which is throwing the exception. The line 'con.open()' is throwing the mentioned exception. Please find the connection string used
private readonly string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=Yes;MAXSCANROWS=0;IMEX=1\";";
public DataTable GetSheetData(string sheetName)
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(this.filePath);
DataTable excelData = new DataTable();
excelData.Locale = CultureInfo.InvariantCulture;
using (OleDbConnection con = new OleDbConnection(string.Format(CultureInfo.CurrentCulture, this.connectionString, this.filePath)))
{
con.Open();
if (!string.IsNullOrEmpty(sheetName))
{
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(string.Format(CultureInfo.CurrentCulture, "select * from [{0}$]", sheetName), con))
{
dataAdapter.Fill(excelData);
}
}
}
return excelData;
}
I figured out a workaround by programatically opening the excel file and saving it by Interop. Now I am able to upload the excel file without any errors.
var ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = ExcelApp.Workbooks.Open(filePath);
workbook.Save();
workbook.Close();
ExcelApp.Quit();
I get the Excel sheet with data uploaded from UI, I need to access that sheet through WCF service and insert the data contained in it into a SQL table.
Kindly guide how this can be done.
I am facing challenge in reading that Excel sheet through WCF service.
I have used below code to read a Excel sheet from a local drive. Change Source property to your FTP location of your excel sheet.
string con = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MyData.xlsx;" +
#"Extended Properties='Excel 8.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using (OleDbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Do your things here
}
}
}
Note: .xls has data limitations whereas .xlsx supports more data rows in a sheet. You need to install Microsoft Office Access database engine 2007 if you are going to read .xlsx files
string UploadedFilePath = FullPathOfExcelOnTheServer;
string ExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + UploadedFilePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1;FMT=Delimited\"";
using (OleDbConnection oledbConnExcel = new OleDbConnection(ExcelConn))
{
oledbConnExcel.Open();
using (OleDbDataAdapter oledbAdapterExcel = new OleDbDataAdapter("SELECT * from [" + SheetName + "$]", oledbConnExcel))
{
using (DataTable dtblSheetData = new DataTable())
{
try
{
oledbAdapterExcel.Fill(dtblSheetData);
}
catch (Exception lexQuery)
{
}
}
}
oledbConnExcel.Close();
}
Fact is the is no way to read the excel data through WCF Service. But You can cosider this approaches
You may:
Read all excel data on client side and call WCF service and include all data from excel in parameter as (array of strings)?
You can create macro(VB) in Excel sheet and what call WCF Service and send data through parameter. When You have all logic in one place.
If client side is .NET side You can use atribbute [KnowsTypeAtribbute].Then You can call WCF service with param type as Excel type (Sheet or other). May by use some type form OpenXml dll may by usefull (But I don't know is sheet or other types is serializable. A serializable is require)
You can send to WFC service path to file and read excel file from that path through WCF service.
I am trying to read excel file in c# however it gives me 'External table is not in the expected format.' error.
Below is my sample code
string con =#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + reportname + #".xls;" +
#"Extended Properties='Excel 8.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from ["+reportname+"$]", connection);
using (OleDbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{...
...
}}}
The strange this is when I try to open the excel manually and save it back, it gives me a pop-up saying 'The report.xls may contain features that are not compatible with web page.Do you want to keep the workbook in this format?' If i select yes the file size reduce from 2.5MB to 10KB.
Now if I run my code, it also works fine.
What should be done in this scenario, as I cannot keep saving the file manually to make it work?
It's my first time to work with c# and I have a task to create a simple Console Application where you simply just click the program button created in the debug file and then export an amount data from the Stored Precedure I created in the database to an Excel file.
My Methode looks like this :
static void Main(string[] args)
{
string path = #"\Users\sa\mydocuments\test.xls";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
//creating the file contents
}
}
using (StreamWriter sw = File.CreateText(path))
{
SqlConnection cn = new SqlConnection("Data Source=server1234;Initial Catalog=MySqlDatabase;Integrated Security=True;Trusted_Connection=True");
SqlCommand cmd = new SqlCommand("MySqlDataBase.dbo.sp_export_data", cn);
cmd.CommandType = CommandType.StoredProcedure;
try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
sw.WriteLine(dr["name"].ToString() + "\t" + dr["size"].ToString());
}
Console.WriteLine("Database has been exported.");
}
catch (Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
}
My program runs and creates an excel file, the problem is it's empty and I have no idea if my code is wrong because im not getting any errors. Not sure if im missing anything in my connectionString etc.
Maybe someone could help me out with this or link to how create an export function for Console Application with stored precedures.
In order to write to Excel files, you'll need to use an Excel library (either the Microsoft Library or a 3rd party).
See here - Creating xls/xlsx files in c#
I am trying to import a .csv file into my database. I am able to import an excel worksheet into my database, however due to different file format as .csv as from .xls, I need to make an import function specially for .csv.
Below is my code:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// Get the name of the Excel spreadsheet to upload.
string strFileName = Server.HtmlEncode(FileUpload1.FileName);
// Get the extension of the Excel spreadsheet.
string strExtension = Path.GetExtension(strFileName);
// Validate the file extension.
if (strExtension != ".xls" && strExtension != ".xlsx" && strExtension != ".csv" && strExtension != ".csv")
{
Response.Write("<script>alert('Failed to import DEM Conflicting Role Datasheet. Cause: Invalid Excel file.');</script>");
return;
}
// Generate the file name to save.
string strUploadFileName = #"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
// Save the Excel spreadsheet on server.
FileUpload1.SaveAs(strUploadFileName);
// Create Connection to Excel Workbook
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Text;";
using (OleDbConnection ExcelConnection = new OleDbConnection(connStr)){
OleDbCommand ExcelCommand = new OleDbCommand("SELECT [columns] FROM +userrolelist", ExcelConnection);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
ExcelConnection.Open();
using (DbDataReader dr = ExcelCommand.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=<IP>;Initial Catalog=<DB>;User ID=<userid>;Password=<password>";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "DEMUserRoles";
bulkCopy.WriteToServer(dr);
Response.Write("<script>alert('DEM User Data imported');</script>");
}
}
}
}
else Response.Write("<script>alert('Failed to import DEM User Roles Data. Cause: No file found.');</script>");
}
The file has been successfully saved, but the error says that the path for the file is not valid, even though the file has been successfully saved as .csv, therefore I am not able to continue with the process of importing the data into my database.
Below are the screenshots of my error:
In conclusion I am having the error that the file path which the csv file is saved is not valid, although the csv file is successfully saved. Need some help from experienced. Thank You
The connection string Data Source should only contain the path to your CSV file. It should not contain the CSV file name.
The file name is specified in the SQL statement as a table.
string dir = #"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\";
string mycsv = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=Text;";
OleDbCommand ExcelCommand = new OleDbCommand(
"SELECT [columns] FROM " + mycsv, ExcelConnection);
I strongly NOT recommend you to use OLE to access to Excel documents. There's an infinite number of glitches and bugs.
In general, using sql to access the data when different cells of one column can contain different data types - is nonsense. But even without this there are enough bugs.
Use COM objects for Excel. Otherwise, you will have a hard - trust me on the floor :-)