Step 1:- (Create DBF File.aspx.cs)(Complete Code)
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Create_DBF_File :
System.Web.UI.Page
{
OleDbConnection dBaseConnection
= null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateDBFFile();
}
}
private DataTable GenerateData()
{
DataTable dt
= new DataTable();
DataRow dr
= null;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(DateTime)));
int totalRow
= 25;
for (int i
= 0; i < totalRow; i++)
{
dr = dt.NewRow();
dr["Column1"] = i + 1;
dr["Column2"] = "Row" + i;
dr["Column3"] = "Row" + i;
dr["Column4"] = DateTime.Now;
dt.Rows.Add(dr);
}
return dt;
}
private void CreateDBFFile()
{
string filepath
= null;
filepath = Server.MapPath("~//Download//");
string TableName
= "T" + DateTime.Now.ToLongTimeString().Replace(":", "").Replace("AM", "").Replace("PM", "");
using(dBaseConnection
= new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
" + " Data Source=" +
filepath + "; " + "Extended
Properties=dBase IV"))
{
dBaseConnection.Open();
OleDbCommand olecommand
= dBaseConnection.CreateCommand();
if ((System.IO.File.Exists(filepath
+ "" + TableName + ".dbf")))
{
System.IO.File.Delete(filepath + "" +
TableName + ".dbf");
olecommand.CommandText = "CREATE TABLE [" +
TableName + "] ([Column1] int, [Column2] varchar(10),
[Column3] varchar(10), [Column4] datetime)";
olecommand.ExecuteNonQuery();
}
else
{
olecommand.CommandText = "CREATE TABLE [" +
TableName + "] ([Column1] int, [Column2] varchar(10),
[Column3] varchar(10), [Column4] datetime)";
olecommand.ExecuteNonQuery();
}
OleDbDataAdapter oleadapter
= new OleDbDataAdapter(olecommand);
OleDbCommand oleinsertCommand
= dBaseConnection.CreateCommand();
foreach (DataRow dr in GenerateData().Rows)
{
string Column1
= dr["Column1"].ToString();
string Column2
= dr["Column2"].ToString();
string Column3
= dr["Column3"].ToString();
DateTime Column4
= Convert.ToDateTime(dr["Column4"]);
oleinsertCommand.CommandText = "INSERT INTO [" +
TableName + "] ([Column1],
[Column2],[Column3],[Column4]) VALUES ('" + Column1 + "','" +
Column2 + "','" + Column3 + "','" +
Column4 + "')";
oleinsertCommand.ExecuteNonQuery();
}
}
FileStream sourceFile
= new FileStream(filepath + "" +
TableName + ".dbf", FileMode.Open);
float FileSize
= 0;
FileSize =
sourceFile.Length;
byte[]
getContent = new byte[Convert.ToInt32(Math.Truncate(FileSize))];
sourceFile.Read(getContent, 0, Convert.ToInt32(sourceFile.Length));
sourceFile.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/dbf";
Response.AddHeader("Content-Length",
getContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment;
filename=Demo.dbf;");
Response.BinaryWrite(getContent);
Response.Flush();
System.IO.File.Delete(filepath + "" +
TableName + ".dbf");
Response.End();
}
}
}
Note :-
Don't forgate to create Download folder inside
your project root folder.
If you are getting the below error:-
The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the
local machine.
Means, In your local machine AccessDatabaseEngine not
registered.
So, You need to download it from Microsoft web site check
following link for downloading AccessDatabaseEngine
Thanks alot..
ReplyDeleteexport unicode font , how to do this
ReplyDelete