赞
踩
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using Excel = Microsoft.Office.Interop.Excel;
namespace PMC_Excel_Email_Automate.RPA
{
public partial class SAP_Stock_Demo : Form
{
public SAP_Stock_Demo()
{
InitializeComponent();
}
ServiceReference1.WebService1SoapClient myWebservice = new ServiceReference1.WebService1SoapClient();
private void button1_Click(object sender, EventArgs e)
{
try {
Excel.Application myExcel = (Excel.Application)Interaction.GetObject(null, "Excel.Application");
for (int i = 1; i <= myExcel.Workbooks.Count; i++)
{
if (myExcel.Workbooks[i].Name == "export.XLSX")
{
myExcel.Workbooks[i].Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);return;
}
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Sheets sheets;
object oMissiong = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Workbook workbook = null;
DataTable dt = new DataTable();
string path4 = @"\\Mlxsztvwpiis01\sap\StockDemo\上架单.xlsx";
Microsoft.Office.Interop.Excel.Application app_1 = new Microsoft.Office.Interop.Excel.Application();
app_1.Visible = true;
app_1.UserControl = true;
Microsoft.Office.Interop.Excel.Workbooks workbooks_1 = app_1.Workbooks;
Microsoft.Office.Interop.Excel._Workbook workbook_1 = workbooks_1.Add(path4); //加载模板
Microsoft.Office.Interop.Excel.Sheets sheets_1 = workbook_1.Sheets;
Microsoft.Office.Interop.Excel._Worksheet worksheet_1 = (Microsoft.Office.Interop.Excel._Worksheet)sheets_1.get_Item(1); //第一个工作薄。
byte[] attachment = null;
string attachmentname = "";
try
{
string excelFilePath = @"\\Mlxsztvwpiis01\sap\StockDemo\export.XLSX";
if (app == null) return;
workbook = app.Workbooks.Open(excelFilePath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong,
oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
sheets = workbook.Worksheets;
//将数据读入到DataTable中
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);//读取第一张表
if (worksheet == null) return;
int iRowCount = worksheet.UsedRange.Rows.Count;
int iColCount = worksheet.UsedRange.Columns.Count;
//生成列头
bool hasTitle = true;
for (int i = 0; i < iColCount; i++)
{
var name = "column" + i;
if (hasTitle)
{
var txt = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Text.ToString();
if (!string.IsNullOrWhiteSpace(txt)) name = txt;
}
while (dt.Columns.Contains(name)) name = name + "_1";//重复行名称会报错。
dt.Columns.Add(new DataColumn(name, typeof(string)));
}
//生成行数据
Microsoft.Office.Interop.Excel.Range range;
int rowIdx = hasTitle ? 2 : 1;
for (int iRow = rowIdx; iRow <= iRowCount; iRow++)
{
DataRow dr = dt.NewRow();
for (int iCol = 1; iCol <= iColCount; iCol++)
{
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, iCol];
dr[iCol - 1] = (range.Value2 == null) ? "" : range.Text.ToString();
}
dt.Rows.Add(dr);
}
int i1 = 5;
for (int i = 0; i < dt.Rows.Count; i++)
{
if(dt.Rows[i]["Stock Category"].ToString() != "Q")
{
if(dt.Rows[i]["Storage Unit"].ToString() == "")
{
worksheet_1.Cells[i1 + 1, 2] = dt.Rows[i]["Material"].ToString();
worksheet_1.Cells[i1 + 1, 3] = dt.Rows[i]["Batch"].ToString();
worksheet_1.Cells[i1 + 1, 4] = dt.Rows[i]["Available stock"].ToString();
worksheet_1.Cells[i1 + 1, 5] = dt.Rows[i]["Storage Type"].ToString();
worksheet_1.Cells[i1 + 1, 7] = dt.Rows[i]["Storage Bin"].ToString();
i1 = i1 + 1;
}
else
{
worksheet_1.Cells[i1 + 1, 2] = dt.Rows[i]["Material"].ToString();
worksheet_1.Cells[i1 + 1, 3] = dt.Rows[i]["Batch"].ToString();
worksheet_1.Cells[i1 + 1, 4] = dt.Rows[i]["Available stock"].ToString();
worksheet_1.Cells[i1 + 1, 5] = dt.Rows[i]["Storage Type"].ToString();
worksheet_1.Cells[i1 + 1, 7] = dt.Rows[i]["Storage Unit"].ToString();
i1 = i1 + 1;
}
}
}
worksheet_1.PrintPreview();
bool issuccess = myWebservice.SendMail(texfromto.Text, texnickname.Text, txtreceive.Text, txtcc.Text, txtsubject.Text, txtbody.Text, attachment, attachmentname, attachment, attachmentname,2);
if (issuccess == true)
{
MessageBox.Show("Email sent succeeded");
}
else
{
MessageBox.Show("Email sent failed");
}
button1.BackColor = Color.Green;
return;
}
catch(Exception ex) { MessageBox.Show(ex.Message); return; }
finally
{
workbook.Close(false, oMissiong, oMissiong);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
app_1.Quit();
app_1 = null;
workbook_1 = null;
worksheet_1 = null;
}
}
private void button2_Click(object sender, EventArgs e)
{
string vbsFile = @"\\Mlxsztvwpiis01\sap\StockDemo\SAP_Stock.vbs";
//string saproute = @"C:\Program Files (x86)\SAP\FrontEnd\SapGui\saplogon.exe";
try
{
//System.Diagnostics.Process p1 = System.Diagnostics.Process.Start(saproute);
//p1.WaitForExit();
System.Diagnostics.Process p = System.Diagnostics.Process.Start(vbsFile);
p.WaitForExit();
button2.BackColor = Color.Green;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);return;
}
finally
{
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
KillProcess("saplogon");
button3.BackColor = Color.Green;
//KillProcess("SAP");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
/// <summary>
/// 杀掉FoxitReader进程
/// </summary>
/// <param name="strProcessesByName"></param>
public static void KillProcess(string processName)
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName.Contains(processName))
{
try
{
p.Kill();
p.WaitForExit(); // possibly with a timeout
MessageBox.Show($"已关闭{processName}进程!!!");
}
catch (Win32Exception e)
{
MessageBox.Show(e.Message.ToString());
}
catch (InvalidOperationException e)
{
MessageBox.Show(e.Message.ToString());
}
}
}
}
private void button4_Click(object sender, EventArgs e)
{
button1.BackColor = Color.LightGray;
button2.BackColor = Color.LightGray;
button3.BackColor = Color.LightGray;
}
private void SAP_Stock_Demo_Load(object sender, EventArgs e)
{
button1.BackColor = Color.LightGray;
button2.BackColor = Color.LightGray;
button3.BackColor = Color.LightGray;
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。