引入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(" "); //获取所有行 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(" "); } } 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日