You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
5.6 KiB
125 lines
5.6 KiB
using Microsoft.Office.Interop.Excel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Excel = Microsoft.Office.Interop.Excel;
|
|
|
|
namespace RS.Common
|
|
{
|
|
public class MicrosoftExcel
|
|
{
|
|
public async void ExportToExcel(System.Data.DataTable dataTable, string path, int function)
|
|
{
|
|
await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
// 创建Excel应用程序对象
|
|
Excel.Application excelApp = new Excel.Application();
|
|
excelApp.Visible = false;
|
|
|
|
// 创建工作簿和工作表对象
|
|
Excel.Workbook workbook = excelApp.Workbooks.Add();
|
|
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
|
|
|
|
if (function == 1)
|
|
{
|
|
worksheet.Cells[1, 1] = "编号";
|
|
worksheet.Cells[1, 2] = "作业配方";
|
|
worksheet.Cells[1, 3] = "批次";
|
|
worksheet.Cells[1, 4] = "产品id";
|
|
worksheet.Cells[1, 5] = "操作员";
|
|
worksheet.Cells[1, 6] = "片数";
|
|
worksheet.Cells[1, 7] = "完成状态";
|
|
worksheet.Cells[1, 8] = "开始时间";
|
|
worksheet.Cells[1, 9] = "结束时间";
|
|
|
|
// 添加数据行
|
|
for (int i = 0; i < dataTable.Rows.Count; i++)
|
|
{
|
|
worksheet.Cells[i + 2, 1] = dataTable.Rows[i][dataTable.Columns[0].ColumnName];
|
|
worksheet.Cells[i + 2, 2] = dataTable.Rows[i][dataTable.Columns[1].ColumnName];
|
|
worksheet.Cells[i + 2, 3] = dataTable.Rows[i][dataTable.Columns[2].ColumnName];
|
|
worksheet.Cells[i + 2, 4] = dataTable.Rows[i][dataTable.Columns[3].ColumnName];
|
|
worksheet.Cells[i + 2, 5] = dataTable.Rows[i][dataTable.Columns[4].ColumnName];
|
|
worksheet.Cells[i + 2, 6] = dataTable.Rows[i][dataTable.Columns[5].ColumnName];
|
|
worksheet.Cells[i + 2, 7] = dataTable.Rows[i][dataTable.Columns[6].ColumnName];
|
|
worksheet.Cells[i + 2, 8] = dataTable.Rows[i][dataTable.Columns[7].ColumnName];
|
|
worksheet.Cells[i + 2, 9] = dataTable.Rows[i][dataTable.Columns[8].ColumnName];
|
|
}
|
|
}
|
|
else if (function == 2)
|
|
{
|
|
worksheet.Cells[1, 1] = "报警编号";
|
|
worksheet.Cells[1, 2] = "报警内容";
|
|
worksheet.Cells[1, 3] = "报警时间";
|
|
worksheet.Cells[1, 4] = "恢复时间";
|
|
|
|
// 添加数据行
|
|
for (int i = 0; i < dataTable.Rows.Count; i++)
|
|
{
|
|
worksheet.Cells[i + 2, 1] = dataTable.Rows[i][dataTable.Columns[0].ColumnName];
|
|
worksheet.Cells[i + 2, 2] = dataTable.Rows[i][dataTable.Columns[1].ColumnName];
|
|
worksheet.Cells[i + 2, 3] = dataTable.Rows[i][dataTable.Columns[2].ColumnName];
|
|
worksheet.Cells[i + 2, 4] = dataTable.Rows[i][dataTable.Columns[3].ColumnName];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
worksheet.Cells[1, 1] = "操作时间";
|
|
worksheet.Cells[1, 2] = "操作用户";
|
|
worksheet.Cells[1, 3] = "操作信息";
|
|
|
|
// 添加数据行
|
|
for (int i = 0; i < dataTable.Rows.Count; i++)
|
|
{
|
|
worksheet.Cells[i + 2, 1] = dataTable.Rows[i][dataTable.Columns[0].ColumnName];
|
|
worksheet.Cells[i + 2, 2] = dataTable.Rows[i][dataTable.Columns[1].ColumnName];
|
|
worksheet.Cells[i + 2, 3] = dataTable.Rows[i][dataTable.Columns[2].ColumnName];
|
|
}
|
|
}
|
|
|
|
worksheet.StandardWidth = 15;
|
|
// 设置单元格格式,防止科学计数法
|
|
Range range = worksheet.get_Range("A2", "A" + (dataTable.Rows.Count + 1).ToString());
|
|
range.NumberFormat = "@"; // 文本格式,或者你可以使用 "0.00" 或其他数字格式
|
|
|
|
// 保存并关闭工作簿
|
|
workbook.SaveAs(path);
|
|
workbook.Close();
|
|
|
|
// 释放COM对象并退出Excel应用程序
|
|
ReleaseObject(worksheet);
|
|
ReleaseObject(workbook);
|
|
excelApp.Quit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.ToString();
|
|
}
|
|
});
|
|
}
|
|
|
|
private void ReleaseObject(object obj)
|
|
{
|
|
try
|
|
{
|
|
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
|
|
obj = null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
obj = null;
|
|
Console.WriteLine("Exception Occurred while releasing object " + ex.ToString());
|
|
}
|
|
finally
|
|
{
|
|
GC.Collect();
|
|
}
|
|
}
|
|
}
|
|
}
|