May 5,2006
Insert new worksheet into a Excel file, without Excel installed!!! (.NET Framework)
最近有需要對 excel 檔案作一些處理,除了一般的讀取資料之外,還需要在 excel 檔案中插入新的工作表(worksheet)。聆聽谷歌(google)與翻閱MSDN之後,終於有了一些解答,不過還是有一些有待改進的地方。
如果需要產生一個單一工作表(single worksheet)的Excel檔案的話,可以用HTML的方式蒙混過去,但是要做成多工作表(multiple worksheets)就不行了。 (註:可以用xml格式,不過就只能在Excel 2003開啟,在這之前的Excel版本不支援)
在聆聽谷歌的過程中,有找到一些利用Excel.Application這個物件來做的,不過用這樣的方式必須在程式執行的機器上頭安裝Excel,所以也行不通。
最後找到的方式是使用OleDB的方式來做,不過用這個方式有一個缺點,就是必須先準備一個空的Excel file(至少會包含一個worksheet),然後透過SQL statement的create table就可以對這個Excel file做新增工作表的動作啦 :D
後來在 http://www.asp.net/ 找到了一個 ExcelXMLWriter 的物件可以使用,用這個方式好多了,不用先準備一個空的Excel file。不過老實說,這個物件的method寫得有點怪,用起來不太順手 :(
最後的最後,在網路上找到可以用BIFF5這個小工具來產生一個空的Excel file,改天再來試試看吧,奮鬥六個鐘頭下來已經快趴了 Orz
(可能得改成 ATL COM,然後再用DLL Import才能在.NET裡頭用)
附註一: OleDb 連結物件程式碼
using System;
using System.Data;
using System.Data.OleDb;
namespace CS_Class
{
/// <summary>
/// OleDBConn 的摘要描述。
/// </summary>
public class OleDBConn
{
private OleDbCommand cmd;
private OleDbDataAdapter adp;
private OleDbConnection cn;
public System.Int32 ErrorCode;
public System.String ErrorMessage;
~OleDBConn()
{
}
#region Constructor
public OleDBConn(System.String strMDB_FilePath)
{
try
{
cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strMDB_FilePath + ";Extended Properties=Excel 8.0;");
cmd = new OleDbCommand();
adp = new OleDbDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
ErrorCode = 0;
ErrorMessage = "";
}
catch (Exception e)
{
ErrorCode = 1;
ErrorMessage = e.Source + e.Message;
}
}
#endregion Constructor
//Close Connection
public void CloseDB()
{
cn.Close();
cn.Dispose();
cn = null;
cmd.Dispose();
cmd = null;
adp.Dispose();
adp = null;
}
#region Execute a query
//Execute a query,and place the query result in ds(TableName)
public void Exec(System.String strQuery, System.String TableName, ref DataSet ds)
{
try
{
cmd.CommandText = strQuery;
adp.SelectCommand = cmd;
adp.Fill(ds, TableName);
ErrorCode = 0;
ErrorMessage = "";
cmd.Connection.Close();
}
catch (Exception e)
{
ErrorCode = 2;
ErrorMessage = e.Source + e.Message;
}
}
//Execute a query
public void Exec(System.String strQuery)
{
try
{
cmd.CommandText = strQuery;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
ErrorCode = 0;
ErrorMessage = "";
cmd.Connection.Close();
}
catch (Exception e)
{
ErrorCode = 2;
ErrorMessage = e.Source + e.Message;
cmd.Connection.Close();
}
}
#endregion
}
}
附註二: Sample Code using Oledb
Private strsql As String
Private Shared ds As New DataSet
Private Sub create()
Dim oleconn As New OleDBConn(Server.MapPath("Book1.xls"), OleDBConn.OleDBType.ExcelXLS)
Try
strsql = "CREATE TABLE test (f1 varchar(20), f2 int);"
oleconn.Exec(strsql)
strsql = "INSERT INTO test values ('老闆快給錢', 100);"
oleconn.Exec(strsql)
strsql = "Select * from test"
oleconn.Exec(strsql, "result", ds)
Response.Write(ds.Tables("result").Rows(0).Item(0))
Catch ex As Exception
Response.Write(oleconn.ErrorMessage)
End Try
End Sub
附註三: Use CarlosAg.ExcelXmlWriter
Imports CarlosAg.ExcelXmlWriter
Private strsql As String
Private Shared ds As New DataSet
Private Sub createdelete()
Dim book As Workbook = New Workbook
Dim sheet As Worksheet = book.Worksheets.Add("Sample")
Dim row As WorksheetRow = sheet.Table.Rows.Add()
row.Cells.Add("Hello World")
sheet = book.Worksheets.Add("hehe")
row = sheet.Table.Rows.Add()
row.Cells.Add("ccc")
book.Save(Server.MapPath("./") & "\test.xls")
End Sub
引用URL
http://cgi.blog.roodo.com/trackback/1540003