Anda di halaman 1dari 2

using System;

using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;
namespace MobileProject.Classes
{
class clsExcel
{
private string sql_string = "";
private string text_header = "";
public clsExcel(string sql_string,string text_header)
{
this.sql_string = sql_string;
this.text_header = text_header;
}
private string good_value(string input)
{
return ((input == "" || (input[0] >= '0' && input[0] <= '9')) ? "'"
: "") + input;
}
public void save()
{
//try
{
System.Windows.Forms.SaveFileDialog opendlg = new System.Windows
.Forms.SaveFileDialog();
opendlg.FileName = Classes.clsPublicVariables.intToday.ToString(
);
opendlg.Filter = "Excel Spreadsheets (*.xls)|*.xls";
opendlg.DefaultExt = "xls";
if (opendlg.ShowDialog() == System.Windows.Forms.DialogResult.OK
)
{
StringBuilder sbrHTML = new StringBuilder();
SqlCommand dc = new SqlCommand(this.sql_string, Classes.clsC
onnection.cn);
Classes.clsConnection.openConnection();
SqlDataReader dr;
dr = dc.ExecuteReader();
//Making HTML
sbrHTML.Append("<meta http-equiv=\"Content-Type\" content=\"
text/html;charset=UTF-8\"><style>.text { mso-number-format:\\@; } </style><TABLE
class=\"text\" Border=1 ID=\"Table1\">");
sbrHTML.Append("<TR bgcolor=\"#d7ffd7\"><TD ColSpan=4><Font
Size=5> " + this.text_header + "</Font></TD><TR>");
sbrHTML.Append("<TR bgcolor=\"#bbbbbb\" >");
for (int i = 0; i < dr.FieldCount; i++)
{
sbrHTML.Append("<TH>" + dr.GetName(i) + "</TH>");
}
sbrHTML.Append("</TR>");
while (dr.Read())
{
sbrHTML.Append("<TR>");
for (int i = 0; i < dr.FieldCount; i++)
{
sbrHTML.Append("<TD>" + good_value(dr.GetValue(i).To
String()) + "</TD>");
}
sbrHTML.Append("</TR>");
}
dr.Close();
sbrHTML.Append("</TABLE>");
sbrHTML.Append("<BR><BR><B>Reported by " + Classes.clsPublic
Variables.user_name + "</B>");
sbrHTML.Append("<BR><BR><B>Date: " + Classes.clsPublicVariab
les.strToday + "</B>");
//ENDOF MAKING HTML
//WRITING AS AN XSL
StreamWriter swXLS = new StreamWriter(opendlg.FileName);
swXLS.Write(sbrHTML.ToString());
swXLS.Close();
Classes.clsConnection.closeConnection();
}
}
//catch (Exception ex)
//{
//System.Windows.Forms.MessageBox.Show(ex.Message, Classes.clsPu
blicVariables.MessageHeader, System.Windows.Forms.MessageBoxButtons.OK, System.W
indows.Forms.MessageBoxIcon.Information, System.Windows.Forms.MessageBoxDefaultB
utton.Button1, System.Windows.Forms.MessageBoxOptions.RightAlign | System.Window
s.Forms.MessageBoxOptions.RtlReading);
Classes.clsConnection.closeConnection();
//}
//OPENING THE XSL FILE
}
}
}