Click here to Skip to main content
15,890,282 members
Home / Discussions / C#
   

C#

 
GeneralRe: finding dynamic IP of running client Pin
Guffa5-Jul-06 0:55
Guffa5-Jul-06 0:55 
QuestionGenerating a Typed DataSet from an XSD-file in VS 2005 Pin
anderslundsgard4-Jul-06 21:11
anderslundsgard4-Jul-06 21:11 
AnswerRe: Generating a Typed DataSet from an XSD-file in VS 2005 Pin
DIMPLE_R4-Jul-06 21:44
DIMPLE_R4-Jul-06 21:44 
QuestionOffice 2007 Toolbar Ribbon Pin
cecildt4-Jul-06 20:56
cecildt4-Jul-06 20:56 
QuestionXML Includes in C# code Pin
urbane.tiger4-Jul-06 20:50
urbane.tiger4-Jul-06 20:50 
QuestionDataGrid to Excel in Windows app. Pin
SysJey4-Jul-06 20:33
SysJey4-Jul-06 20:33 
AnswerRe: DataGrid to Excel in Windows app. Pin
Chetan Ranpariya4-Jul-06 21:11
Chetan Ranpariya4-Jul-06 21:11 
AnswerRe: DataGrid to Excel in Windows app. Pin
Azoli5-Jul-06 20:25
Azoli5-Jul-06 20:25 
using System;
using System.IO;
using System.Data;
using System.Windows.Forms;

namespace AzoControls.MyDataExport
{
public class AzoDataExportToXLS
{
private int rowCount = 0;
private int sheetCount = 1;
private System.IO.StreamWriter excelDoc;
const string endExcelXML = "";
const string startExcelXML = "<xml version="">\r\n<workbook "="" +
="" "xmlns="\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n"" xmlnsBlush | :O ="\"urn:schemas-microsoft-com:office:office\"\r\n" "xmlns:x="\"urn:schemas-" microsoft-com:office:"="" "excel\"\r\n="" xmlns:ss="\"urn:schemas-microsoft-com:"" "office:spreadsheet\"="">\r\n <styles>\r\n " +
"\r\n " +
"<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" +
"\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +
"\r\n <Protection/>\r\n \r\n " +
"\r\n <Font " +
"x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n \r\n " +
"\r\n <NumberFormat" +
" ss:Format=\"@\"/>\r\n \r\n \r\n <NumberFormat " +
"ss:Format=\"0.00\"/>\r\n \r\n " +
"\r\n <NumberFormat " +
"ss:Format=\"0\"/>\r\n \r\n \r\n <NumberFormat " +
"ss:Format=\"dd/mm/yyyy;@\"/>\r\n \r\n " +
"\r\n ";

private DataTable dtXLS;
private DataRow drXLS;

public AzoDataExportToXLS(DataTable parTableName)
{
//Parameter DataTable Object
this.dtXLS = parTableName;
}

public void ExportToXLS()
{
try
{
string strFileName = "File001";
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.InitialDirectory = Path.GetDirectoryName(strFileName);
saveDlg.FileName = Path.GetFileNameWithoutExtension(strFileName);
saveDlg.AddExtension = true;
saveDlg.Filter = "Windows Excel (*.xls)|*.xls";
if (saveDlg.ShowDialog() == DialogResult.OK)
{
try
{
this.exportToExcel(saveDlg.FileName.ToString());
//this.WriteDataToXLS(saveDlg.FileName.ToString());
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message.ToString());
}
}
private void exportToExcel(string fileName)
{
excelDoc = new System.IO.StreamWriter(fileName);
excelDoc.Write(startExcelXML);
excelDoc.Write("<worksheet ss:name="\"DataPages"" +="" sheetcount.tostring().trim()="" "\"="">");
excelDoc.Write("");
excelDoc.Write("<row>");

for (int ic = 0; ic <= dtXLS.Columns.Count-1; ic++)
{
excelDoc.Write("<cell ss:styleid="\"BoldColumn\"">");
excelDoc.Write(dtXLS.Columns[ic].Caption.ToString());
excelDoc.Write("
");
}
excelDoc.Write("");

for (int x = 0; x <= dtXLS.Rows.Count-1; x++)
{
rowCount++;
drXLS = dtXLS.Rows[x];
//if row > 64000 new page
if (rowCount == 64000)
{
rowCount = 0;
sheetCount++;
excelDoc.Write("
");
excelDoc.Write(" ");
excelDoc.Write("<worksheet ss:name="\"Sheet"" +="" sheetcount="" "\"="">");
excelDoc.Write("");
}
excelDoc.Write("<row>"); //ID=" + rowCount + "

for (int y = 0; y < this.dtXLS.Columns.Count; y++)
{
System.Type cellType;
cellType = drXLS[y].GetType();

switch (cellType.ToString())
{
case "System.String":
string XLSParameter = drXLS[y].ToString();
XLSParameter = XLSParameter.Trim();
XLSParameter = XLSParameter.Replace("&", "&");
XLSParameter = XLSParameter.Replace(">", ">");
XLSParameter = XLSParameter.Replace("<", "<");
excelDoc.Write("<cell ss:styleid="\"StringLiteral\"">" +
"");
excelDoc.Write(XLSParameter);
excelDoc.Write("
");
break;
case "System.DateTime":
DateTime XMLDate = (DateTime)drXLS[y];
string XMLDatetoString = ""; //Excel Converted Date
XMLDatetoString = XMLDate.Year.ToString() +
"-" +
(XMLDate.Month < 10 ? "0" +
XMLDate.Month.ToString() : XMLDate.Month.ToString()) +
"-" +
(XMLDate.Day < 10 ? "0" +
XMLDate.Day.ToString() : XMLDate.Day.ToString()) +
"T" +
(XMLDate.Hour < 10 ? "0" +
XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) +
":" +
(XMLDate.Minute < 10 ? "0" +
XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) +
":" +
(XMLDate.Second < 10 ? "0" +
XMLDate.Second.ToString() : XMLDate.Second.ToString()) +
".000";
excelDoc.Write("<cell ss:styleid="\"DateLiteral\"">" +
"");
excelDoc.Write(XMLDatetoString);
excelDoc.Write("
");
break;
case "System.Boolean":
excelDoc.Write("<cell ss:styleid="\"StringLiteral\"">" +
"");

excelDoc.Write(drXLS[y].ToString());
excelDoc.Write("
");
break;
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.Byte":
excelDoc.Write("<cell ss:styleid="\"Integer\"">" +
"");

excelDoc.Write(drXLS[y].ToString());
excelDoc.Write("
");
break;
case "System.Decimal":
excelDoc.Write("<cell ss:styleid="\"Decimal\"">" +
"");

excelDoc.Write(drXLS[y].ToString());
excelDoc.Write("
");
break;
case "System.Double":
excelDoc.Write("<cell ss:styleid="\"Decimal\"">" +
"");

excelDoc.Write(drXLS[y].ToString());
excelDoc.Write("
");
break;
case "System.DBNull":
excelDoc.Write("<cell ss:styleid="\"StringLiteral\"">" +
"");
excelDoc.Write("");
excelDoc.Write("
");
break;
default:
throw (new Exception(cellType.ToString() + " not handled."));
}
}
excelDoc.Write("");
}
excelDoc.Write("
");
excelDoc.Write(" ");
excelDoc.Write(endExcelXML);
excelDoc.Close();
}
}
}


grandy
GeneralRe: DataGrid to Excel in Windows app. Pin
Azoli5-Jul-06 20:48
Azoli5-Jul-06 20:48 
QuestionDisplay UI controls in Datagrid cell (Its urget pls) Pin
Kais4U4-Jul-06 20:11
Kais4U4-Jul-06 20:11 
QuestionSerialization with different Class-Versions Pin
Tomerland4-Jul-06 20:09
Tomerland4-Jul-06 20:09 
QuestionDebug ActiveX control from C# application [modified] Pin
Maddie from Dartford4-Jul-06 19:58
Maddie from Dartford4-Jul-06 19:58 
QuestionHow to apply certain commands during installation [modified] Pin
Kamran Younus4-Jul-06 19:32
Kamran Younus4-Jul-06 19:32 
Question3 Tier Architecture Code Pin
bml36224-Jul-06 19:12
bml36224-Jul-06 19:12 
AnswerRe: 3 Tier Architecture Code Pin
Praveen Nayak4-Jul-06 22:14
Praveen Nayak4-Jul-06 22:14 
GeneralClickety Police Pin
Colin Angus Mackay4-Jul-06 22:20
Colin Angus Mackay4-Jul-06 22:20 
QuestionPurpose of boxing and unboxing? Pin
Anthony Potts4-Jul-06 18:14
Anthony Potts4-Jul-06 18:14 
AnswerRe: Purpose of boxing and unboxing? Pin
Guffa4-Jul-06 19:02
Guffa4-Jul-06 19:02 
QuestionHow to register .NET assembly Pin
Duong Tien Nam4-Jul-06 17:30
Duong Tien Nam4-Jul-06 17:30 
AnswerRe: How to register .NET assembly Pin
S. Senthil Kumar4-Jul-06 19:18
S. Senthil Kumar4-Jul-06 19:18 
Questionmultiselection of month calendar Pin
toink toink4-Jul-06 15:59
toink toink4-Jul-06 15:59 
AnswerRe: multiselection of month calendar Pin
mav.northwind4-Jul-06 19:46
mav.northwind4-Jul-06 19:46 
Questionplease help Pin
omar el halwagy4-Jul-06 14:35
omar el halwagy4-Jul-06 14:35 
AnswerRe: please help Pin
J4amieC4-Jul-06 22:00
J4amieC4-Jul-06 22:00 
Questionenmurator Pin
omar el halwagy4-Jul-06 14:32
omar el halwagy4-Jul-06 14:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.