Click here to Skip to main content
15,891,951 members
Home / Discussions / C#
   

C#

 
AnswerRe: NTFS & FAT direct access Pin
Henry Minute9-Aug-09 5:01
Henry Minute9-Aug-09 5:01 
GeneralRe: NTFS & FAT direct access Pin
Mark Brend10-Aug-09 1:44
Mark Brend10-Aug-09 1:44 
QuestionC# and SQL deployment issue Pin
labdakos9-Aug-09 3:22
labdakos9-Aug-09 3:22 
AnswerRe: C# and SQL deployment issue Pin
pelnor9-Aug-09 5:04
pelnor9-Aug-09 5:04 
AnswerRe: C# and SQL deployment issue Pin
pelnor9-Aug-09 5:11
pelnor9-Aug-09 5:11 
QuestionRead XML File and load result in datagridview Pin
Member 47429229-Aug-09 2:31
Member 47429229-Aug-09 2:31 
AnswerRe: Read XML File and load result in datagridview Pin
Eddy Vluggen9-Aug-09 4:52
professionalEddy Vluggen9-Aug-09 4:52 
QuestionHow to Export Contents of DataTable and DataGridView in Excel without using Excel object. Pin
sharad Pyakurel9-Aug-09 1:36
sharad Pyakurel9-Aug-09 1:36 
I have written code to export content of DatagridView to Excel using Strem. It works but in some case some texts are found missing. And Numeric Datas are not formatted(i.e. while adding or calculating from exported data, unable to perform calculation. My code is like this:

public class ExcelReport
{
#region
private string Heading1;
private DataGridView Grid;
private int ReportType;

private int StartCol = 0;
private int StartRow = 0;
#endregion

#region Constructors
/// <summary>
/// Report Type:Excel=1,HTML=2,Word=3 and All=4
/// </summary>
/// <param name="_heading1"></param>
/// <param name="_grid"></param>
public ExcelReport(string _heading1,int _reportType, DataGridView _grid)
{
this.Heading1 = _heading1;
this.ReportType = _reportType;
this.Grid = _grid;
WriteReport();
}
#endregion

#region Main Function For Exporting
private void WriteReport()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (ReportType == 1)
{
saveFileDialog1.Filter = "Excel Worksheet files|*.xls";
}
else if (ReportType == 2)
{
saveFileDialog1.Filter = "HTML Files|*.htm";
}
else if (ReportType == 3)
{
saveFileDialog1.Filter = "Word Files|*.doc";
}
else if (ReportType == 4)
{
saveFileDialog1.Filter = "Excel Worksheet files|*.xls|HTML Files|*.html|Word Files|*.doc|All Files|*.*";
}

//Exporting To Different Formates using Stream
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileInfo fi = new FileInfo(saveFileDialog1.FileName);
StreamWriter sw = fi.CreateText();
sw.WriteLine("<table bgcolor='#FFFFFF' border=1 width='100%'>");
sw.Write("<tr>");
foreach (DataGridViewColumn col in Grid.Columns)
{
string FontName;
if (Grid.ColumnHeadersDefaultCellStyle.Font.Name.ToString() != null)
{
FontName = Grid.ColumnHeadersDefaultCellStyle.Font.Name.ToString();
}
else
{
FontName = col.DefaultCellStyle.Font.Name.ToString();
}
sw.Write("<th bgcolor='#ECE9D8'><FONT FACE='" + FontName + "'>" +ReplaceSpclChars(col.HeaderText) + "</font></th>");
}
sw.Write("</tr>");

//Fills cells of excel sheet by the content of grid view
int i=0;
for (i = 0; i < Grid.Rows.Count; i++)
{
sw.Write("<tr>");
for (int j = 0; j < Grid.Columns.Count; j++)
{
string FontName;
if (Grid.Columns[j].DefaultCellStyle.Font != null)
{
FontName = Grid.Columns[j].DefaultCellStyle.Font.Name.ToString();
}
else
{
FontName = Grid.DefaultCellStyle.Font.Name.ToString();
}

if (Grid[j, i].Value != null)
{
sw.Write("<td><FONT FACE='" + FontName + "'>" + ReplaceSpclChars(Grid[j, i].Value.ToString()) + "</font></td>");
}
else
{
sw.Write("<td><FONT FACE='" + FontName + "'>" + Grid[j, i].Value + "</font></td>");
}
}
sw.Write("</tr>");
}
sw.WriteLine("<b><Font Face='Shangrila Numeric' size='14px'>" + ReplaceSpclChars(Heading1)+ "</Font></b>\n");
sw.WriteLine("</table>");
sw.Close();
if (i > 1)
{
CrudeFx.MsgBoxNepali.Show(@";""rL PS;]ndf n}hfg] sfd ;DkGg eof] M", MessageBoxButtons.OK);
}
}
#endregion
}

private string ReplaceSpclChars(string fieldName)
{
fieldName = fieldName.Replace("<", "&lt;");
fieldName = fieldName.Replace(">", "&gt;");
return fieldName;
}
}
AnswerRe: How to Export Contents of DataTable and DataGridView in Excel without using Excel object. Pin
Infarkt9-Aug-09 3:31
Infarkt9-Aug-09 3:31 
AnswerRe: How to Export Contents of DataTable and DataGridView in Excel without using Excel object. Pin
sharad Pyakurel10-Aug-09 1:55
sharad Pyakurel10-Aug-09 1:55 
Questionproblem writing to a file: The process cannot access the file 'c:\scene.xml' because it is being used by another process Pin
reilak9-Aug-09 1:28
reilak9-Aug-09 1:28 
AnswerRe: problem writing to a file: The process cannot access the file 'c:\scene.xml' because it is being used by another process Pin
Luc Pattyn9-Aug-09 1:33
sitebuilderLuc Pattyn9-Aug-09 1:33 
GeneralRe: problem writing to a file: The process cannot access the file 'c:\scene.xml' because it is being used by another process Pin
reilak9-Aug-09 1:45
reilak9-Aug-09 1:45 
GeneralRe: problem writing to a file: The process cannot access the file 'c:\scene.xml' because it is being used by another process Pin
Luc Pattyn9-Aug-09 1:47
sitebuilderLuc Pattyn9-Aug-09 1:47 
GeneralRe: problem writing to a file: The process cannot access the file 'c:\scene.xml' because it is being used by another process Pin
reilak9-Aug-09 1:50
reilak9-Aug-09 1:50 
GeneralRe: problem writing to a file: The process cannot access the file 'c:\scene.xml' because it is being used by another process Pin
Luc Pattyn9-Aug-09 1:55
sitebuilderLuc Pattyn9-Aug-09 1:55 
GeneralRe: problem writing to a file: The process cannot access the file 'c:\scene.xml' because it is being used by another process Pin
reilak9-Aug-09 2:03
reilak9-Aug-09 2:03 
GeneralRe: problem writing to a file: The process cannot access the file 'c:\scene.xml' because it is being used by another process Pin
Luc Pattyn9-Aug-09 2:15
sitebuilderLuc Pattyn9-Aug-09 2:15 
QuestionCan I get the code of any *.exe file?? Pin
CoderForEver8-Aug-09 9:01
CoderForEver8-Aug-09 9:01 
AnswerRe: Can I get the code of any *.exe file?? Pin
harold aptroot8-Aug-09 9:47
harold aptroot8-Aug-09 9:47 
AnswerRe: Can I get the code of any *.exe file?? Pin
Not Active8-Aug-09 11:16
mentorNot Active8-Aug-09 11:16 
GeneralRe: Can I get the code of any *.exe file?? Pin
CoderForEver9-Aug-09 3:48
CoderForEver9-Aug-09 3:48 
GeneralRe: Can I get the code of any *.exe file?? Pin
Not Active9-Aug-09 4:51
mentorNot Active9-Aug-09 4:51 
GeneralRe: Can I get the code ...?? No! Pin
Luc Pattyn9-Aug-09 5:29
sitebuilderLuc Pattyn9-Aug-09 5:29 
GeneralRe: Can I get the code of any *.exe file?? Pin
CoderForEver10-Aug-09 1:58
CoderForEver10-Aug-09 1:58 

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.