using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class SaveResult
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlsWorkBook;
Microsoft.Office.Interop.Excel.Worksheet newWorksheet,Base;
bool IsSheetExist = false;
public void SaveResultReport(int ProductType,int ProductNum)//保存报表
{
//MessageBox.Show(System.AppDomain.CurrentDomain.BaseDirectory);//基目录
xlApp.DisplayAlerts = false;
xlApp.Visible = false;
xlApp.ScreenUpdating = false;
switch (ProductType)
{
case 0://A
switch (ProductNum)
{
case 0://增速
xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\A增速.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
break;
case 1://续航
xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\A续航.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
break;
case 2://壳体
xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\A壳体.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
break;
}
break;
case 1://B
switch (ProductNum)
{
case 0://增速
xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\B增速.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
break;
case 1://续航
xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\B续航.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
break;
case 2://壳体
xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\B壳体.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
break;
}
break;
}
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in xlsWorkBook.Worksheets)
{
if (sheet.Name == DateTime.Now.ToString("yyyyMMdd"))
IsSheetExist = true;
else if (sheet.Name =="Base")//基础表
{
Base = sheet;
}
}
if (!IsSheetExist)
{
//newWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlsWorkBook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//添加新表
// newWorksheet.Name = DateTime.Now.ToString("yyyyMMdd");
Base.Copy(xlsWorkBook.Worksheets[1]);
newWorksheet = xlsWorkBook.Worksheets[1];
newWorksheet.Name = DateTime.Now.ToString("yyyyMMdd");
}
else
{
newWorksheet = xlsWorkBook.Worksheets[1];
MessageBox.Show("已存在表单");
}
xlsWorkBook.Save();
try
{
xlsWorkBook.Close(null, null, null);
xlApp.Quit();
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in process)
{
if (p.ProcessName.ToLower() == "EXCEL.exe")
{
p.Kill();
}
}
GC.Collect();
}
catch (Exception)
{
//正常退出Excel失败,可以记录一下日志.
MessageBox.Show("退出Excel失败");
}
}
}
C#_无插件读取Excel_拷贝表并访问
最新推荐文章于 2023-03-22 16:26:08 发布