1. 程式人生 > 實用技巧 >NX二次開發-C# NPOI庫讀寫EXCEL

NX二次開發-C# NPOI庫讀寫EXCEL

引入NPOI庫

NX9+VS2012

using System;
using NXOpen;
using NXOpen.UF;
using NPOI;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;

public class Program
{
    // class members
    private static Session theSession;
    private static UI theUI;
    private static
UFSession theUfSession; public static Program theProgram; public static bool isDisposeCalled; //------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------ public Program() {
try { theSession = Session.GetSession(); theUI = UI.GetUI(); theUfSession = UFSession.GetUFSession(); isDisposeCalled = false; } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here -----
// UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message); } } //------------------------------------------------------------------------------ // Explicit Activation // This entry point is used to activate the application explicitly //------------------------------------------------------------------------------ public static int Main(string[] args) { int retValue = 0; try { theProgram = new Program(); //TODO: Add your application code here //讀取EXCEL //路徑 string path = "D:\\3DPNG\\123.xlsx"; //讀取 FileStream fs = File.OpenRead(path); IWorkbook wk = new XSSFWorkbook(fs); theUfSession.Ui.OpenListingWindow(); //獲得所有sheet for (int i = 0; i < wk.NumberOfSheets; i++) { //讀取當前表資料 ISheet sheet = wk.GetSheetAt(i); theUfSession.Ui.WriteListingWindow("sheet" + i.ToString() + "所有內容"); theUfSession.Ui.WriteListingWindow("\n"); //獲取所有行 for (int j = 0; j <= sheet.LastRowNum; j++) { IRow row = sheet.GetRow(j);//讀取當前行資料 if (row != null) { //獲得當前行的所有列 for (int k = 0; k < row.LastCellNum; k++) { //讀取單元格資料(行和列) ICell cell = sheet.GetRow(j).GetCell(k); //列印 theUfSession.Ui.WriteListingWindow(cell.ToString()); theUfSession.Ui.WriteListingWindow(","); } } theUfSession.Ui.WriteListingWindow("\n"); } } fs.Close(); //新建寫入EXCEL //路徑 string path1 = "D:\\3DPNG\\456.xlsx"; //建立EXCEL FileStream fs2 = File.Create(path1); IWorkbook wk2 = new XSSFWorkbook(); //建立sheet ISheet sheet1 = wk2.CreateSheet("A"); ISheet sheet2 = wk2.CreateSheet("B"); //寫入內容 ICell tmpCell1 = sheet1.CreateRow(1).CreateCell(1); tmpCell1.SetCellValue("這是sheet1!"); ICell tmpCell2 = sheet2.CreateRow(1).CreateCell(1); tmpCell2.SetCellValue("這是sheet2"); //設定當前sheet wk2.SetActiveSheet(1); //將圖片新增到workbook中 指定圖片格式 返回圖片所在workbook->Picture陣列中的索引地址(從1開始) string PngPath = "D:\\3DPNG\\1.png"; byte[] byteArray = System.IO.File.ReadAllBytes(PngPath); int pictureIdx = wk2.AddPicture(byteArray, PictureType.PNG); //在sheet2中建立畫布 IDrawing patriarch = sheet2.CreateDrawingPatriarch(); //設定錨點 (在起始單元格的X座標0-1023,Y的座標0-255,在終止單元格的X座標0-1023,Y的座標0-255,起始單元格行數,列數,終止單元格行數,列數) IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, 5, 5, 20, 20); //建立圖片 IPicture pict = patriarch.CreatePicture(anchor, pictureIdx); //自動調節圖片大小 //pict.Resize(); //另存為 wk2.Write(fs2); fs2.Close(); theProgram.Dispose(); } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here ----- } return retValue; } //------------------------------------------------------------------------------ // Following method disposes all the class members //------------------------------------------------------------------------------ public void Dispose() { try { if (isDisposeCalled == false) { //TODO: Add your application code here } isDisposeCalled = true; } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here ----- } } public static int GetUnloadOption(string arg) { //Unloads the image explicitly, via an unload dialog //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly); //Unloads the image immediately after execution within NX return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately); //Unloads the image when the NX session terminates // return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination); } } Caesar盧尚宇 2020年8月16日