Lớp ThisAddIn đóng vai trò là điểm khởi đầu của ứng dụng, thực hiện các thao tác khởi tạo thông qua phương thức InternalStartup().
private void InternalStartup()
{
this.Startup += new System.EventHandler(InitializeAddIn);
this.Shutdown += new System.EventHandler(CleanupResources);
}
Trong hàm xử lý sự kiện khởi tạo, thực hiện các thao tác khởi động ban đầu như tạo giao diện điều khiển
Tạo panel tùy chỉnh và xác định vị trí hiển thị
public CustomTaskPane customPanel = null;
private void InitializeAddIn(object sender, System.EventArgs e)
{
CustomPanelControl panelControl = new CustomPanelControl();
customPanel = this.CustomTaskPanes.Add(panelControl, " ");
customPanel.Width = 600;
customPanel.Visible = false;
customPanel.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft;
}
Định nghĩa vị trí hiển thị panel
customPanel.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft;
Khởi tạo dữ liệu cho ComboBox
// Khởi tạo danh sách ngành nghề
public void PopulateIndustryComboBox()
{
LoadIndustryData();
Dictionary<string, string> industryMap = new Dictionary<string, string>();
industryMap.Add("A", "Năng lượng - Nguyên liệu - Máy công nghiệp");
industryMap.Add("B", "Thực phẩm");
industryMap.Add("C", "Hàng tiêu dùng (Thuốc lá)");
industryMap.Add("D", "Đồ uống không cồn");
// Gán dữ liệu cho ComboBox
this.industryComboBox.DataSource = ConvertToDataTable(industryMap);
this.industryComboBox.DisplayMember = "name";
this.industryComboBox.ValueMember = "value";
}
private DataTable ConvertToDataTable(Dictionary<string, string> data) {
DataTable table = new DataTable();
table.Columns.Add("name");
table.Columns.Add("value");
foreach (var item in data)
{
DataRow row = table.NewRow();
row[0] = item.Value;
row[1] = item.Key;
table.Rows.Add(row);
}
return table;
}
Cách tạo Dictionary
public static Dictionary<string, string> headerMapping = new Dictionary<string, string> {
{"projectName", "Tên dự án"},
{"jobNo", "Số công việc"},
{"clientName", "Tên khách hàng"},
{"researchType", "Loại khảo sát"},
{"industryType", "Ngành nghề"}
};
Dictionary<string, string> industryList = new Dictionary<string, string>();
industryList.Add("A", "Năng lượng - Nguyên liệu - Máy công nghiệp");
industryList.Add("B", "Thực phẩm");
industryList.Add("C", "Hàng tiêu dùng (Thuốc lá)");
industryList.Add("D", "Đồ uống không cồn");
Phân tích chuỗi JSON
Cần thêm tham chiếu trước khi sử dụng
using Newtonsoft.Json;
// Phân tích chuỗi JSON
JsonResult result = JsonConvert.DeserializeObject<JsonResult>(jsonContent);
Lấy workbook hiện tại
Excel.Workbook currentWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook;
Lấy sheet hiện hành
Excel.Worksheet activeSheet = Globals.ThisAddIn.Application.ActiveSheet;
Ngăn hiện tượng rung màn hình khi nhập liệu
activeSheet.Application.ScreenUpdating = false;
activeSheet.Application.ScreenUpdating = true;
Xóa nội dung bảng tính
activeSheet.UsedRange.Value = null;
Định dạng kích thước ô
activeSheet.UsedRange.Rows.RowHeight = 15;
activeSheet.Rows[3].RowHeight = 15;
activeSheet.Columns.ColumnWidth = 4;
activeSheet.Columns[1].ColumnWidth = 15;
Thiết lập màu nền
// Không màu
activeSheet.UsedRange.Interior.ColorIndex = 0;
// Màu tùy chỉnh
activeSheet.UsedRange.Interior.Color = Color.FromArgb(220, 198, 224);
Thiết lập đường viền
using Spire.Xls;
// Không viền
activeSheet.UsedRange.Borders.LineStyle = LineStyleType.None;
// Viền mảnh
activeSheet.UsedRange.Borders.LineStyle = LineStyleType.Hair;
Thay đổi hướng chữ
// Chữ dọc
range.Orientation = Excel.XlOrientation.xlVertical;
// Căn chỉnh trên
range.VerticalAlignment = Excel.XlVAlign.xlVAlignTop;
Gửi yêu cầu GET
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiEndpoint);
request.Method = "GET";
request.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
string responseData = reader.ReadToEnd();
reader.Close();
responseStream.Close();