实现类似于Golden32导出Excel的一个类。
1、调用Excel直接打开导出结果
2、直接用块的方式写入Excel,提高了性能
3、数字型、日期型的数据能自动识别
4、设置单元格的格式用先列再行的方式,上万行记录也轻松对付。
因工作的需要,要把人事系统的查询结果导出到Excel中。
其中工号、日期字段导到Excel就变成了数值型和英文的日期格式,需要再手工设置格式,很麻烦。
参考了网上导出Excel的各种文章,解决了一些性能问题,终于完成了这个类~~
public static void Dst2Excel(ref DataSet _dst)
{
try
{
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
MessageBox.Show("Can't find Excel Object!");
return;
}
if (_dst == null || _dst.Tables.Count <= 0)
{
MessageBox.Show("无显示数据,请查询库数据或导入数据!!!");
return;
}
xlApp.Visible = true;
object missing = System.Reflection.Missing.Value;
int countcolumns = _dst.Tables[0].Columns.Count;
int countrows = _dst.Tables[0].Rows.Count;
string A = Get_TableHeader(countcolumns);
object[,] myvalues1 = new object[countrows + 1, countcolumns];
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook theWorkbook = workbooks.Add(missing);
Excel.Sheets sheets = theWorkbook.Worksheets;
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
Excel.Range range = worksheet.get_Range("A1", A + (countrows + 1).ToString());
// 标题行
for (int j = 0; j < countcolumns; j++)
{
myvalues1[0, j] = _dst.Tables[0].Columns[j].Caption;
}
// 内容行
for (int j = 0; j < countcolumns; j++)
{
switch (_dst.Tables[0].Columns[j].DataType.ToString())
{
case "System.String":
worksheet.get_Range(worksheet.Cells[1, j + 1], worksheet.Cells[countrows + 1, j + 1]).NumberFormatLocal = "@";
break;
case "System.DateTime":
worksheet.get_Range(worksheet.Cells[1, j + 1], worksheet.Cells[countrows + 1, j + 1]).NumberFormatLocal = "yyyy-mm-dd";
break;
}
for (int i = 1; i < countrows + 1; i++)
{
myvalues1[i, j] = _dst.Tables[0].Rows[i - 1][j];
}
}
range.Cells.Value2 = myvalues1;
workbooks = null;
theWorkbook = null;
sheets = null;
worksheet = null;
range = null;
xlApp = null;
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
finally
{
GC.Collect();
}
}
public static string Get_TableHeader(int num)
{
string A = "";
for (int i = 0; i < num; i++)
{
if ((65 + i) < 91)
A = ((char)(65 + i)).ToString();
else
A = "A" + ((char)(i + 39)).ToString(); //i+39:Z后的字符转成A、B
}
return A;
}
{
try
{
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
MessageBox.Show("Can't find Excel Object!");
return;
}
if (_dst == null || _dst.Tables.Count <= 0)
{
MessageBox.Show("无显示数据,请查询库数据或导入数据!!!");
return;
}
xlApp.Visible = true;
object missing = System.Reflection.Missing.Value;
int countcolumns = _dst.Tables[0].Columns.Count;
int countrows = _dst.Tables[0].Rows.Count;
string A = Get_TableHeader(countcolumns);
object[,] myvalues1 = new object[countrows + 1, countcolumns];
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook theWorkbook = workbooks.Add(missing);
Excel.Sheets sheets = theWorkbook.Worksheets;
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
Excel.Range range = worksheet.get_Range("A1", A + (countrows + 1).ToString());
// 标题行
for (int j = 0; j < countcolumns; j++)
{
myvalues1[0, j] = _dst.Tables[0].Columns[j].Caption;
}
// 内容行
for (int j = 0; j < countcolumns; j++)
{
switch (_dst.Tables[0].Columns[j].DataType.ToString())
{
case "System.String":
worksheet.get_Range(worksheet.Cells[1, j + 1], worksheet.Cells[countrows + 1, j + 1]).NumberFormatLocal = "@";
break;
case "System.DateTime":
worksheet.get_Range(worksheet.Cells[1, j + 1], worksheet.Cells[countrows + 1, j + 1]).NumberFormatLocal = "yyyy-mm-dd";
break;
}
for (int i = 1; i < countrows + 1; i++)
{
myvalues1[i, j] = _dst.Tables[0].Rows[i - 1][j];
}
}
range.Cells.Value2 = myvalues1;
workbooks = null;
theWorkbook = null;
sheets = null;
worksheet = null;
range = null;
xlApp = null;
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
finally
{
GC.Collect();
}
}
public static string Get_TableHeader(int num)
{
string A = "";
for (int i = 0; i < num; i++)
{
if ((65 + i) < 91)
A = ((char)(65 + i)).ToString();
else
A = "A" + ((char)(i + 39)).ToString(); //i+39:Z后的字符转成A、B
}
return A;
}