SoFunction
Updated on 2025-03-06

Detailed explanation of C# exporting Excel

This example shares the specific code for C# exporting Excel for your reference. The specific content is as follows

using System;
using ;
using ;
using ;
using ;
using ;

namespace DMS
{
/// <summary>
/// C#Operate Excel class/// </summary>
class ExcelOperate
{
//Faste one//public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
//{
//  DataTable dataTable = [0];
//  int rowNumber = ;
//  int columnNumber = ;

//  if (rowNumber == 0)
//  {
// ("No data can be imported into Excel files!");//    return false;
//  }

// // Create Excel object//   excel = new ();
//  (true);
// = isShowExcle;// Whether to open the Excel file
// //Fill in data//  for (int c = 0; c < rowNumber; c++)
//  {
//    for (int j = 0; j < columnNumber; j++)
//    {
//      [c + 1, j + 1] = [c].ItemArray[j];
//    }
//  }

//  return true;
//}


//Method 2//public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
//{
//  DataTable dataTable = [0];
//  int rowNumber = ;

//  int rowIndex = 1;
//  int colIndex = 0;


//  if (rowNumber == 0)
//  {
//    return false;
//  }

// // Create Excel object//   excel = new ();
//  (true);
//   = isShowExcle;

// // Generate field name//  foreach (DataColumn col in )
//  {
//    colIndex++;
//    [1, colIndex] = ;
//  }

// //Fill in data//  foreach (DataRow row in )
//  {
//    rowIndex++;
//    colIndex = 0;
//    foreach (DataColumn col in )
//    {
//      colIndex++;
//      [rowIndex, colIndex] = row[];
//    }
//  }

//  return true;
//}

//Mahas 3 (fastest speed)/// <summary>
/// Export data from the dataset to an EXCEL file/// </summary>
/// <param name="dataSet">Input dataset</param>/// <param name="isShowExcle">Whether to display the EXCEL file</param>/// &lt;returns&gt;&lt;/returns&gt;
public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
{
DataTable dataTable = [0];
int rowNumber = ;//Excluding field namesint columnNumber = ;
int colIndex = 0;

if (rowNumber == 0) 
{
return false;
}

//Create Excel object excel = new ();
//(true);
 workbook = ();
 worksheet = ()[1];
 = isShowExcle;
// worksheet = ()[1];
 range;

//Generate field nameforeach (DataColumn col in )
{
colIndex++;
[1, colIndex] = ;
}

object[,] objData = new object[rowNumber, columnNumber]; 

for (int r = 0; r &lt; rowNumber; r++) 
{
for (int c = 0; c &lt; columnNumber; c++)
{
objData[r, c] = [r][c];
}
//();
}

// Write to Excelrange = worksheet.get_Range([2, 1], [rowNumber + 1, columnNumber]);
// = "@";//Set the cell to text formatrange.Value2 = objData;
worksheet.get_Range([2, 1], [rowNumber + 1, 1]).NumberFormat = "yyyy-m-d h:mm";

return true; 
}

//Method Four//public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
//{
//  DataTable dataTable = [0];
//  int rowNumber = ;
//  int columnNumber = ;
//  String stringBuffer = "";

//  if (rowNumber == 0)
//  {
// ("No data can be imported into Excel files!");//    return false;
//  }

// // Create Excel object//   excel = new ();
//  (true);
// = isShowExcle;// Whether to open the Excel file
// //Fill in data//  for (int i = 0; i &lt; rowNumber; i++)
//  {
//    for (int j = 0; j &lt; columnNumber; j++)
//    {
//      stringBuffer += [i].ItemArray[j].ToString();
//      if (j &lt; columnNumber - 1)
//      {
//        stringBuffer += "\t";
//      }
//    }
//    stringBuffer += "\n";
//  }
//  ();
//  (stringBuffer);
//  (()[1, 1]).Select();
//  (()).Paste(, );
//  ();

//  return true;
//}

//public bool DataSetToExcel(DataSet dataSet, string fileName, bool isShowExcle)
//{
//  DataTable dataTable = [0];
//  int rowNumber = ;
//  int columnNumber = ;

//  if (rowNumber == 0)
//  {
// ("No data can be imported into Excel files!");//    return false;
//  }

// // Create Excel object//   excel = new ();
//   workBook = (true);
// = false;// Whether to open the Excel file
// //Fill in data//  for (int i = 0; i &lt; rowNumber; i++)
//  {
//    for (int j = 0; j &lt; columnNumber; j++)
//    {
//      [i + 1, j + 1] = [i].ItemArray[j];
//    }
//  }

//  //string fileName = path + "\\" + ().Replace(':', '_') + ".xls";
//  (fileName, , , , , , , , , , , );

//  try
//  {
//     = true;
//     = false;
//    //();
//  }
//  catch (Exception exception)
//  {
//    ();
//  }
//  finally
//  {
//    (, , );
//    ();
//  }

//  if (isShowExcle)
//  {
//    (fileName);
//  }
//  return true;
//}

//public bool DataSetToExcel(DataSet dataSet, string fileName, bool isShowExcle)
//{
//  DataTable dataTable = [0];
//  int rowNumber = ;//Excluding field names//  int columnNumber = ;
//  int colIndex = 0;

//  if (rowNumber == 0)
//  {
// ("No data can be imported into Excel files!");//    return false;
//  }

// // Create Excel object//   excel = new ();
//  //(true);
//   workbook = ();
//   worksheet = ()[1];
//   = isShowExcle;
//  // worksheet = ()[1];
// = "deflection data";//   range;

// // Generate field name//  foreach (DataColumn col in )
//  {
//    colIndex++;
//    [1, colIndex] = ;
//  }

//  object[,] objData = new object[rowNumber, columnNumber];

//  for (int r = 0; r &lt; rowNumber; r++)
//  {
//    for (int c = 0; c &lt; columnNumber; c++)
//    {
//      objData[r, c] = [r][c];
//    }
//    //();
//  }

// // Write to Excel//  range = worksheet.get_Range([2, 1], [rowNumber + 1, columnNumber]);
//  // = "@";//Set the cell to text format//  range.Value2 = objData;
//  worksheet.get_Range([2, 1], [rowNumber + 1, 1]).NumberFormat = "yyyy-m-d h:mm";

//  //string fileName = path + "\\" + ().Replace(':', '_') + ".xls";
//  (fileName, , , , , , , , , , , );

//  try
//  {
//     = true;
//     = false;
//    //();
//  }
//  catch (Exception exception)
//  {
//    ();
//  }
//  finally
//  {
//    (, , );
//    ();
//  }

//  //if (isShowExcle)
//  //{
//  //  (fileName);
//  //}
//  return true;
//}

/// &lt;summary&gt;
/// Save data in the dataset to an EXCEL file/// &lt;/summary&gt;
/// <param name="dataSet">Input dataset</param>/// <param name="fileName">Save the absolute path name of the EXCEL file</param>/// <param name="isShowExcle">Whether to open the EXCEL file</param>/// &lt;returns&gt;&lt;/returns&gt;
public bool DataSetToExcel(DataSet dataSet, string fileName, bool isShowExcle)
{
DataTable dataTable = [0];
int rowNumber = ;//Excluding field namesint columnNumber = ;
int colIndex = 0;

if (rowNumber == 0) 
{
("No data can be imported into an Excel file!");
return false;
}

//Create Excel object excel = new ();
//(true);
 workbook = ();
 worksheet = ()[1];
 = false;
// worksheet = ()[1];
 range;

//Generate field nameforeach (DataColumn col in )
{
colIndex++;
[1, colIndex] = ;
}

object[,] objData = new object[rowNumber, columnNumber]; 

for (int r = 0; r &lt; rowNumber; r++) 
{
for (int c = 0; c &lt; columnNumber; c++)
{
objData[r, c] = [r][c];
}
//();
}

// Write to Excelrange = worksheet.get_Range([2, 1], [rowNumber + 1, columnNumber]);
// = "@";//Set the cell to text formatrange.Value2 = objData;
worksheet.get_Range([2, 1], [rowNumber + 1, 1]).NumberFormat = "yyyy-m-d h:mm";

//string fileName = path + "\\" + ().Replace(':', '_') + ".xls"; 
(fileName, , , , , , , , , , , );

try 
{
 = true;
 = false;
//();
}
catch (Exception exception)
{
();
}
finally
{
(, , );
();
}

if (isShowExcle) 
{
(fileName);
}
return true;
}
}
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.