Imports Outlook = Microsoft.Office.Interop.Outlook
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data
Imports CpmBigDataImportService
Public Class Form1
Private dtMain As DataTable
Private Sub Button1_Click(sender As Object, e As EventArgs)
'Dim dir As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
'Dim path = dir + "\" + "export (1).xlsx"
'Dim dt As DataTable = GetData(path)
'dgvMain.DataSource = dt
'tempMai.Recipients.Add()
End Sub
Function FindDl(sName As String) As Outlook.DistListItem
Dim app As New Outlook.Application
Dim flag As Boolean
flag = False
Dim fld As Outlook.Folder, i As Integer, dl As Outlook.DistListItem
fld = app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
For i = fld.Items.Count To 1 Step -1
If "DistListItem" = TypeName(fld.Items.Item(i)) Then
dl = fld.Items.Item(i)
If dl.DLName = sName Then
flag = True
Exit For
End If
End If
Next
If flag Then
Return dl
Else
Return Nothing
End If
End Function
Function ReCreateDl(sName As String) As Outlook.DistListItem
DelDl(sName)
ReCreateDl = CreateDL(sName)
End Function
Function CreateDL(sName As String) As Outlook.DistListItem
Dim app As New Outlook.Application
Dim dli As Outlook.DistListItem = app.CreateItem(Outlook.OlItemType.olDistributionListItem)
dli.DLName = sName
dli.Save()
Return dli
End Function
Sub DelDl(sName As String)
Dim app As New Outlook.Application
Dim fld As Outlook.Folder, i As Integer, dl As Outlook.DistListItem
fld = app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
For i = fld.Items.Count To 1 Step -1
If "DistListItem" = TypeName(fld.Items.Item(i)) Then
dl = fld.Items.Item(i)
If dl.DLName = sName Then dl.Delete()
End If
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RefreshEmployee()
End Sub
Private Sub btnSyn_Click(sender As Object, e As EventArgs) Handles btnSyn.Click
Dim frm As New Form2
frm.ShowDialog()
RefreshEmployee()
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim app As New Outlook.Application
Dim dl As Outlook.DistListItem = ReCreateDl("XXXXXX")
Dim tempMai As Outlook.MailItem = app.CreateItem(Outlook.OlItemType.olMailItem)
For index = 0 To dtMain.Rows.Count - 1
tempMai.Recipients.Add(dtMain.Rows.Item(index).Item("yx").ToString())
Next
dl.AddMembers(tempMai.Recipients)
tempMai = Nothing
dl.Save()
MsgBox("联系人列表已更新!")
End Sub
Private Sub RefreshEmployee()
Dim filename As String = "employee.xml"
Dim myDir As String = AppDomain.CurrentDomain.BaseDirectory
'MyCommon.ExecuteProcess("explorer.exe", myDir)
Dim fileFullName As String = myDir & "" & filename
If System.IO.File.Exists(fileFullName) Then
dtMain = MyCommon.ConvertXmlToDataTable(MyCommon.ReadXmlStringFromFile(filename, "", myDir))
dgvMain.DataSource = dtMain
End If
End Sub
End Class
Imports Excel = Microsoft.Office.Interop.Excel
Imports CpmBigDataImportService
Public Class Form2
Private dtMain As DataTable = New DataTable()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtExcept.Text = "xa,xx"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dir As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
Dim path
'path = dir + "\" + "export (1).xlsx"
'Dim dt As DataTable = GetData(path)
'dgvMain.DataSource = dt
Dim ofd As New OpenFileDialog : ofd.Filter = "xlsx(*.xlsx)|*.xlsx"
ofd.InitialDirectory = dir
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
path = ofd.FileName
dtMain = GetData(path)
dgvMain.DataSource = dtMain
End If
'path = dir + "\" + "export (1).xlsx"
End Sub
Private Function GetData(fileName As String) As DataTable
Dim dt As New DataTable
dt.Columns.Add("xm", System.Type.GetType("System.String"))
dt.Columns.Add("rsfw", System.Type.GetType("System.String"))
dt.Columns.Add("yx", System.Type.GetType("System.String"))
dt.Columns.Item("yx").Unique = True
Dim key(1) As DataColumn
key(0) = dt.Columns.Item("yx")
'dt.PrimaryKey = key
Dim eApp As New Excel.Application
Dim wb As Excel.Workbook = eApp.Workbooks.Open(Filename:=fileName, [ReadOnly]:=True)
Dim sht As Excel.Worksheet = wb.Worksheets(1)
Dim arr As Object(,) = sht.UsedRange.Value2
For row = 2 To arr.GetUpperBound(0)
If arr(row, 13) = Nothing Then
Continue For
End If
Dim dr As DataRow : dr = dt.NewRow()
dr.Item("xm") = arr(row, 3)
dr.Item("rsfw") = arr(row, 5)
dr.Item("yx") = arr(row, 13)
dt.Rows.Add(dr)
Next
wb.Close()
eApp.Quit()
Return dt
End Function
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
Dim strfw = txtExcept.Text
dgvMain.DataSource = dtMain
dgvExcept.DataSource = ClassLibrary1.Class1.GetExceptDt(dtMain, strFw:=strfw)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim filename As String = "employee.xml"
Dim myDir As String = AppDomain.CurrentDomain.BaseDirectory
dtMain.TableName = "dtMain"
MyCommon.WriteToFile(MyCommon.ConvertDataTableToXml(dtMain), filename, 0, myDir)
MsgBox(myDir & "/" & filename & "已保存")
End Sub
End Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CpmBigDataImportService;
using System.Data;
namespace ClassLibrary1
{
public class Class1
{
public static DataTable GetExceptDt(DataTable dt0, string strFw) {
string[] fws = strFw.Split(',');
List<string> ls = new List<string>();
foreach (var item in fws)
{
ls.Add(string.Format(" {0} like '%{1}%'","rsfw",item));
}
string cond = string.Join("or",ls.ToArray());
DataTable dt = dt0.Clone();
DataRow[] drs = dt0.Select(cond);
foreach (DataRow dr in drs)
{
dt.Rows.Add(dr.ItemArray);
dr.Delete();
}
dt0.AcceptChanges();
return dt;
}
}
}