I stumbled upon this great article by Yuan Wang which allowed you to convert an XLS file into CSV in C# (Convert XLS into CSV). It was created and compiled using Visual Studio 2005. I copied the code and compiled it in VS 2008 and it compiled OK but when I ran it, I got the following exception:
C:\Documents and Settings\d205414\My Documents\Visual Studio 2008\Projects\XlsToCsv\XlsToCsv\obj\Release>XlsToCsv.exe System.Data.OleDb.OleDbException: Could not find installable ISAM. at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString cons tr, OleDbConnection connection) at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOpti ons options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection o wningObject) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbC onnection owningConnection, DbConnectionPoolGroup poolGroup) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection ow ningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection ou terConnection, DbConnectionFactory connectionFactory) at System.Data.OleDb.OleDbConnection.Open() at XlsToCsv.Program.convertExcelToCSV(String sourceFile, String worksheetName , String targetFile) in C:\Documents and Settings\d205414\My Documents\Visual St udio 2008\Projects\XlsToCsv\XlsToCsv\Program.cs:line 51
I made a few modifications to the source, to fix the exception but also allow the executable to accept parameters. Here’s the modified source code:
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
namespace XlsToCsv
{
class Program
{
static void Main(string[] args)
{
try
{
string sourceFile, worksheetName, targetFile, skipHeader;
sourceFile = args[0]; worksheetName = "Sheet1"; targetFile = args[1]; skipHeader = args[2];
convertExcelToCSV(sourceFile, worksheetName, targetFile, skipHeader);
}
catch
{
Console.WriteLine("USAGE: XlsToCsv [XLS File] [CSV File] [Skip Header? Yes/No]");
}
}
static void convertExcelToCSV(string sourceFile, string worksheetName, string targetFile, string skipHeader)
{
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + sourceFile + ";" + @"Extended Properties=""Excel 8.0;HDR=" + skipHeader + @";IMEX=1""";
OleDbConnection conn = null;
StreamWriter wrtr = null;
OleDbCommand cmd = null;
OleDbDataAdapter da = null;
try
{
conn = new OleDbConnection(strConn);
conn.Open();
cmd = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", conn);
cmd.CommandType = CommandType.Text;
wrtr = new StreamWriter(targetFile);
da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
for (int x = 0; x < dt.Rows.Count; x++)
{
string rowString = "";
for (int y = 0; y < dt.Columns.Count; y++)
{
rowString += "\"" + dt.Rows[x][y].ToString() + "\",";
}
wrtr.WriteLine(rowString);
}
Console.WriteLine("File converted successfully.");
}
catch
{
// Console.WriteLine(exc.ToString());
Console.WriteLine("USAGE: XlsToCsv [XLS File] [CSV File] [Skip Header? Yes/No]");
Console.ReadLine();
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
cmd.Dispose();
da.Dispose();
wrtr.Close();
wrtr.Dispose();
}
}
}
}
Save the C# code and compile in Visual Studio 2008 (I used express edition myself).
To execute, call the following in command prompt:
USAGE: XlsToCsv [XLS File] [CSV File] [Skip Header? Yes/No]
eg.
XlsToCsv source.XLS output.CSV No
This post is an extension from the URL2File script that I posted previously. While the URL2File script allowed you to save web pages to a local file, the following VbScript will allow you to save a binary file on the web to a local file.
This VbScript requires Windows script hosting which you can download from:
Save the following code as save2file.vbs:
'Wscript.Arguments(0) - Website URL
'Wscript.Arguments(1) - Full File Path
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save binary data.
BinaryStream.Type = adTypeBinary
'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write BinaryGetURL(Wscript.Arguments(0))
'Save binary data To disk
BinaryStream.SaveToFile Wscript.Arguments(1), adSaveCreateOverWrite
Function BinaryGetURL(URL)
'Create an Http object, use any of the four objects
Dim Http
' Set Http = CreateObject("Microsoft.XMLHTTP")
' Set Http = CreateObject("MSXML2.ServerXMLHTTP")
Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
' Set Http = CreateObject("WinHttp.WinHttpRequest")
'Send request To URL
Http.Open "GET", URL, False
Http.Send
'Get response data As a string
BinaryGetURL = Http.ResponseBody
End Function
To run the script in a command prompt session:
save2file.vbs [URL] [FULL OUTPUT FILE PATH]
eg. save2file.vbs http://localhost/test.XLS c:\temp\test.XLS
I recently needed to execute a HTTP post from an ApEx application to another web page running as some sort of a service. For added security the web page service only accepted POST requests and one of the parameters passed is a secret password (not encrypted but secure enough for an Intranet application).
Anyway the requirement was to post the request using PL/SQL to the web service, and receive the response. I visited the oracle page on UTL_HTTP for a bit of reference and came up with the following code:
set serveroutput on;
exec dbms_output.enable(1000000000);
set escape '\'
DECLARE
req UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
value VARCHAR2(1024); -- URL to post to
v_url VARCHAR2(200) := 'http://T97040476TA9000/core_dmt/withdraw_job.php';
-- Post Parameters
v_param VARCHAR2(500) := 'pwd=password123\&core_id=12223\&type=PK\&reason=Test reason';
v_param_length NUMBER := length(v_param);
BEGIN
-- Set up proxy servers if required
-- UTL_HTTP.SET_PROXY('proxy.my-company.com', 'corp.my-company.com');
req := UTL_HTTP.BEGIN_REQUEST (url=> v_url, method => 'POST');
-- UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
UTL_HTTP.SET_HEADER (r => req,
name => 'Content-Type',
value => 'application/x-www-form-urlencoded');
UTL_HTTP.SET_HEADER (r => req,
name => 'Content-Length',
value => v_param_length);
UTL_HTTP.WRITE_TEXT (r => req,
data => v_param); resp := UTL_HTTP.GET_RESPONSE(req);
LOOP
UTL_HTTP.READ_LINE(resp, value, TRUE);
DBMS_OUTPUT.PUT_LINE(value);
END LOOP;
UTL_HTTP.END_RESPONSE(resp);
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
UTL_HTTP.END_RESPONSE(resp);
END;
/
The only thing you need to change to make the code work for you are the v_url and the v_param values to reflect the url of your service and the parameters it expects.
Happy Coding!

Recent Comments