Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have written a code to transfer the data from Data Set to Excel file. Its works fine on the development machine but when i publish it on server and run it from any other machine excel file fails to generate. can any body help me on this ..Is it a coding problem or a permission problem.
I did coding as follows:

C#
public void ExportDataToExcel(DataSet dsData, ExportStyle style)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass excel = null;
            Microsoft.Office.Interop.Excel.Workbooks workbooks = null;
            Microsoft.Office.Interop.Excel.Workbook workbook = null;
            try
            {
                _listrow = new SortedList();
                excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                workbooks = excel.Workbooks;
                workbook = workbooks.Add(true);
                ExportCurrentData(excel, dsData, style);
                _listrow = null;
                Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)excel.ActiveSheet;
                worksheet.Activate();
                excel.Visible = true;
            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                if (workbook != null) Marshal.ReleaseComObject(workbook);
                if (workbooks != null) Marshal.ReleaseComObject(workbooks);
                if (excel != null) Marshal.ReleaseComObject(excel);
                workbook = null;
                workbooks = null;
                excel = null;
            }
        }



thanks in advance.
Posted
Updated 20-Oct-11 3:35am
v2
Comments
Simon Bang Terkildsen 20-Oct-11 10:00am    
If you were to tell us what exception you got and on which line then you might actually find a solution below that were actually able to tell you why it is happening.
Virender Rajput 20-Oct-11 10:11am    
I am not getting any exception or error..its works fine in development machine buy when i publish it on Server and run it from other machine its runs with any error or exception but , excel file is not launched. there is no error or exception i get. Please help. I u have another code to generate excel file from DataSet the please share it with me...

thanks a lot..

Your code is using actual Excel to create the worksheets, this requires Excel to be installed on your server.

There are Excel components which do this and don't require Office to be installed and scale better with multiple users.
 
Share this answer
 
Comments
Simon Bang Terkildsen 20-Oct-11 9:11am    
+5, if you can avoid using Office Interop do it and spare yourself the headache.
Mehdi Gholam 20-Oct-11 9:18am    
Defenitly, thanks.
we have already MS office 2010 installed in our server. But its not helping ..
 
Share this answer
 
Comments
Mehdi Gholam 20-Oct-11 9:36am    
Use the comments if you require more information, don't post solutions.
Have a look at this MSDN article Working with the Office XP Primary Interop Assemblies[^].
 
Share this answer
 
You can alternatively use some open format like XML to do the Excel generation as well. Have a look at these links : TIPS[^]

there are also some open source solutions as well:
http://excelpackage.codeplex.com/
http://simpleooxml.codeplex.com/

and finally our beloved CP has this: Generating Excel (XML Spreadsheet) in C#[^]

Hope this helps

Cheers
 
Share this answer
 
thanks for the reply...
but in which namespace "<b>ExcelWriter</b>" event nis defined..??
 
Share this answer
 
Comments
Simon Bang Terkildsen 20-Oct-11 9:57am    
Read Mehdi's comment to your previous comment you posted as solution.
For one you should not do it and second the person you're asking a question doesn't get notified an thus most likely never sees your question/comment.
// Its excel write try this also add ur solution


using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;

/// <summary>
/// Produces Excel file without using Excel
/// </summary>
[SuppressMessage("Microsoft.StyleCop.CSharp.ReadabilityRules", "SA1101:PrefixLocalCallsWithThis",
Justification = "this is not required")]
[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented",
Justification = "Code is not documented")]
public sealed class ExcelWriter : IDisposable
{
// private Stream stream;
private readonly BinaryWriter writer;

private readonly ushort[] begin = { 0x0809, 8, 0, 0x10, 0, 0 };
private readonly ushort[] end = { 0x0A, 00 };

/// <summary>
/// Initializes a new instance of the <see cref="ExcelWriter"/> class.
/// </summary>
/// <param name="stream">The stream.</param>
public ExcelWriter(Stream stream)
{
// this.stream = stream;
writer = new BinaryWriter(stream);
}

/// <summary>
/// Writes the text cell value.
/// </summary>
/// <param name="row">The row. no</param>
/// <param name="col">The col. no</param>
/// <param name="value">The string value.</param>
public void WriteCell(int row, int col, string value)
{
ushort[] data = { 0x0204, 0, 0, 0, 0, 0 };
var len = value.Length;
var plainText = Encoding.ASCII.GetBytes(value);
data[1] = (ushort)(8 + len);
data[2] = (ushort)row;
data[3] = (ushort)col;
data[5] = (ushort)len;
WriteUshortArray(data);
writer.Write(plainText);
}

/// <summary>
/// Writes the integer cell value.
/// </summary>
/// <param name="row">The row number.</param>
/// <param name="col">The column number.</param>
/// <param name="value">The value.</param>
public void WriteCell(int row, int col, int value)
{
ushort[] data = { 0x027E, 10, 0, 0, 0 };
data[2] = (ushort)row;
data[3] = (ushort)col;
WriteUshortArray(data);
var buffer = (value << 2) | 2;
writer.Write(buffer);
}

/// <summary>
/// Writes the double cell value.
/// </summary>
/// <param name="row">The row number.</param>
/// <param name="col">The column number.</param>
/// <param name="value">The value.</param>
public void WriteCell(int row, int col, double value)
{
ushort[] data = { 0x0203, 14, 0, 0, 0 };
data[2] = (ushort)row;
data[3] = (ushort)col;
WriteUshortArray(data);
writer.Write(value);
}

/*
/// <summary>
/// Writes the empty cell.
/// </summary>
/// <param name="row">The row number.</param>
/// <param name="col">The column number.</param>
public void WriteCell(int row, int col)
{
ushort[] clData = { 0x0201, 6, 0, 0, 0x17 };
clData[2] = (ushort)row;
clData[3] = (ushort)col;
WriteUshortArray(clData);
}
*/

/// <summary>
/// Must be called once for creating XLS file header
/// </summary>
public void BeginWrite()
{
WriteUshortArray(begin);
}

/// <summary>
/// Ends the writing operation, but do not close the stream
/// </summary>
public void EndWrite()
{
WriteUshortArray(end);
writer.Flush();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (disposing)
{
// dispose managed resources
writer.Close();
}
}

private void WriteUshortArray(IList<ushort> value)
{
for (var i = 0; i < value.Count; i++)
{
writer.Write(value[i]);
}
}
}
}
 
Share this answer
 
Comments
I.explore.code 20-Oct-11 10:15am    
Nice one! can you explain those ushort[] arrays as well please?
anil_bang0011 20-Oct-11 10:19am    
No yaar . i just copy past . if this code working Then good other .. not an issue ,
i have also same type problem and using this ..
my problem have Solved
Virender Rajput 20-Oct-11 10:32am    
please try to provide code to generate Excel file from data set in c#.
anil_bang0011 20-Oct-11 11:00am    
add this above code add in ur program class Name ExcelWriter
and below its
methods
private static void WriteExcel(DataTable dataTableToBeConverted, string excelFileName)

provide u datatable and fileName yaar
private static void WriteExcel(DataTable dataTableToBeConverted, string excelFileName)
{
var stream = new FileStream(excelFileName, FileMode.OpenOrCreate);
var writer = new ExcelWriter(stream);
writer.BeginWrite();

// Write Header
for (var i = 0; i < dataTableToBeConverted.Columns.Count; i++)
{
writer.WriteCell(0, i, dataTableToBeConverted.Columns[i].ColumnName);
}

// Write Lines
for (var rows = 0; rows < dataTableToBeConverted.Rows.Count; rows++)
{
for (var columns = 0; columns < dataTableToBeConverted.Columns.Count; columns++)
{
DataRow dr = dataTableToBeConverted.Rows[rows];
if (dr.IsNull(columns) == true)
{
writer.WriteCell(rows + 1, columns, "");
}
else
{
switch (dataTableToBeConverted.Columns[columns].DataType.Name)
{
case "String":
writer.WriteCell(rows + 1, columns, Convert.ToString(dr[columns], CultureInfo.InvariantCulture));
break;
case "Decimal":
case "Double":
writer.WriteCell(rows + 1, columns, Convert.ToDouble(dr[columns], CultureInfo.InvariantCulture));
break;
case "Int32":
writer.WriteCell(rows + 1, columns, Convert.ToInt32(dr[columns], CultureInfo.InvariantCulture));
break;
case "DateTime":
writer.WriteCell(rows + 1, columns, Convert.ToString(dr[columns], CultureInfo.InvariantCulture));
break;

}
}
}
}

writer.EndWrite();
stream.Close();
}


----
Try This i also implement This .
 
Share this answer
 
Comments
I.explore.code 20-Oct-11 9:51am    
as far as I can tell "ExcelWriter" is not a native .NET library. You would also need to provide the source for ExcelWriter only then your solution can work.
Simon Bang Terkildsen 20-Oct-11 9:58am    
Indeed
Virender Rajput 20-Oct-11 10:38am    
in which namespace or assembly is ExcelWriter and cultureInfo is.??

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