Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello frds,

i want to export my table data in excel sheet .i do that and it work. but i want to need that data in one cell of excel. that i don't get.
i have table which have contain 4 field. but i want to get that 4 field data in one cell of excel.....
Posted
Comments
moulinavi 14-Aug-12 14:18pm    
sir, i have a problem in exporting data into excel sheet..
i am not create any excel sheet,my aim is to export data from my c# windows application. i worked on it for 3 days.so, will u plz help me to do the best
here is my code..



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace siva
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
textBox3.Visible = false;
textBox4.Visible = false;
textBox5.Visible = false;
textBox6.Visible = false;
}
int tamt = 0;
int bal = 0;
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
tamt = tamt + int.Parse(textBox3.Text);
}
if (checkBox2.Checked == true)
{
tamt = tamt + int.Parse(textBox4.Text);
}
if (checkBox3.Checked == true)
{
tamt = tamt + int.Parse(textBox5.Text);
}
if (checkBox4.Checked == true)
{
tamt = tamt + int.Parse(textBox6.Text);
}

textBox7.Text = tamt.ToString();
bal = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
textBox8.Text = (bal - tamt).ToString();
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
textBox3.Visible = true;
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
textBox4.Visible = true;
}

private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
textBox5.Visible = true;
}

private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
textBox6.Visible = true;
}

private void button3_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();

}

private void button2_Click(object sender, EventArgs e)
{
Excel.Application xlapp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Excel.Workbook xlworkbook = (Excel.Workbook)xlapp.ActiveWorkbook;
Excel.Worksheet xlworksheet = (Excel.Worksheet)xlworkbook.ActiveSheet;
Excel.Range chart_range = xlworksheet.UsedRange;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 3; j++)
{
xlworksheet.Cells[i + 4, j + 1] = DataGridView.Rows[i].Cells[j].Value;
}
}
}
}
}

sir reply me as soon as possible plzzzz

The following is some code that I have used to export data to excel:

C#
private static void Excel(string fileName, List<IDirectoryInventoryDataCollector> list)
{
    try
    {
        var xlApp = new Excel.Application();
        var xlWorkBook = xlApp.Workbooks.Add();
        var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        ExcelTitleRow(list[0], 1, xlWorkSheet);

        int row = 2;
        foreach (var item in list)
        {
            ExcelFillRow(item, row++, xlWorkSheet);
        }

        for (int i = 1; i < list[0].MaxLevel - 1; i++)
        {
            ((Range)xlWorkSheet.Columns[i]).ColumnWidth = 2;
        }
        ((Range)xlWorkSheet.Columns[list[0].MaxLevel - 1]).ColumnWidth = 30;
        ((Range)xlWorkSheet.Rows[1]).WrapText = true;
        ((Range)xlWorkSheet.Rows[1]).HorizontalAlignment = HorizontalAlignment.Center;
        ((Range)xlWorkSheet.Cells[1, 1]).WrapText = false;

        xlWorkBook.SaveAs(fileName);
        xlWorkBook.Close();
        xlApp.Quit();
    }
    catch (AccessViolationException)
    {
        System.Windows.Forms.MessageBox.Show(
             "Have encountered access violation. This could be issue with Excel 2000 if that is only version installed on computer",
             "Access Violation");
    }
    catch (Exception)
    {
        System.Windows.Forms.MessageBox.Show("Unknown error",
             "Unknown error");
    }
}

private static void ExcelFillRow(IDirectoryInventoryDataCollector item, int row, Excel.Worksheet sheet)
{
    sheet.Cells[row, item.Level] = item.Name;
    int column = item.MaxLevel;
    foreach (var property in item.GetProperties())
    {
        sheet.Cells[row, column++] = property;
    }
}

private static void ExcelTitleRow(IDirectoryInventoryDataCollector item, int row, Excel.Worksheet sheet)
{
    sheet.Cells[row, 1] = "Name";
    int column = item.MaxLevel;
    foreach (var property in item.GetPropertyNames())
    {
        sheet.Cells[row, column++] = property;
    }
}


It will have to be appropriately modified for what you need. You should be able to put tabs, commas, or something else between the fields before inserting into an Excel cell.
 
Share this answer
 
Comments
Maciej Los 12-May-12 18:32pm    
Good work! +5
V!jAy pAnT 19-Jul-12 1:49am    
hi..... i want to export the custom data i.e only selected columns from the dataset ... how it is possible ..plz give me solution......thanx
Clifford Nelson 19-Jul-12 13:55pm    
It is easily possible. If you manually create an excel spreadsheet as shown above, you can be selective about the data you insert in the spreadsheet. I beleive you can also do it by just creating a new query. This may also help: http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx
V!jAy pAnT 31-Jul-12 7:48am    
thanx ........it is very helpful 4 me
Without seeing the code you have done, it's quite hard to say what the problem is, but if you can now add data to four cells, then concatenate the four values and then add the result into a single cell.
 
Share this answer
 
There are several methods for exporting data to Excel.
Maybe you should try with another one having more documentation or tutorials.
If you want transfer data without particular formatting, I suggest you to look at Solution 1 - OLEDB from this article: Exporting Data to Excel[^].
You could revise the standard queries adapting them to your goals.
 
Share this answer
 
can u tell me the query for concatenate the field,,,
here are code for export in Excel and its work.

C#
IDataRepository idrep = new DataRepository();
ExcelWriter objWriter = new ExcelWriter();
            Order objOrder = new Order(idrep);
            DataTable dtOrderList = objOrder.GetPendingOrderList();
            dtOrderList.TableName = "PendingOrders";
            ExcelWriter.ToExcel(dtOrderList, "PendingOrder.xls");
 
Share this answer
 
v2
Comments
Maciej Los 12-May-12 9:53am    
This code seems to be OK, but to execute it, we need to install: Excel_Out package, am i right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900