cvi中使用c#生成的dll

一直很头疼cvi操作excel,很多时候不当的操作会出现很多excel进程导致电脑卡机。用vs2010操作excel报表组件org.in2bits.MyXls则可以避免这类问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.in2bits.MyXls;
namespace myxls
{
    public class Class1
    {
       public struct xlsSet_
        {
            public ushort startIndex;
            public ushort endIndex;
            public ushort width;
            public ushort fontHeight;
        };

        //xlsSet_ xlsSet;
        XlsDocument doc;
        Worksheet sheet;
        ColumnInfo col;
        public int ShowMessage(int a, int b)
        {
            return (a + b);
        }

        public void CreateNewXLS(string fileName, string sheetName)
        {

            doc = new XlsDocument();
            doc.FileName = fileName + ".xls";
            sheet = doc.Workbook.Worksheets.Add(sheetName);

            col = new ColumnInfo(doc, sheet); //创建列样式对象
        }

        public void SaveXLS(string filePath)
        {
            doc.Save(filePath);
        }
        public void SetXLSStyle(xlsSet_ xlsSet)
        {

            col.ColumnIndexStart = xlsSet.startIndex;  //起始列,索引从0开始
            col.ColumnIndexEnd = xlsSet.endIndex;    //结束列,索引从0开始,这样为第1列、第2列使用此样式
            col.Width = xlsSet.width;         //宽度,字节长度,ushort类型 0~65535

            sheet.AddColumnInfo(col);  //将列样式作用于此工作表

            XF xf = doc.NewXF(); //单元格样式对象
            xf.VerticalAlignment = VerticalAlignments.Centered; //垂直居中
            xf.HorizontalAlignment = HorizontalAlignments.Centered;  //水平居中

            xf.Pattern = 1; //填充风格,0为无色填充,1为没有间隙的纯色填充
            xf.PatternColor = Colors.Green; //填充背景底色

            xf.Font.ColorIndex = 5; //字体前景色颜色,未知值
            xf.Font.FontName = "微软雅黑"; //字体 
            xf.Font.Height = xlsSet.fontHeight;//20 * 20; //字体大小 
            xf.UseBorder = true; //使用边框 
            xf.BottomLineStyle = 1; //边框样式 
            xf.BottomLineColor = Colors.Red; //边框颜色 

        }

        public void OpenXLS(string fileName)
        {
            System.Diagnostics.Process.Start("excel.exe", fileName);   //直接打开文件Readme.txt  
        }

        public void WriteData(int column, int line, string content)
        {
            Cells cells = sheet.Cells;

            cells.Add(column, line, content);
        }
        //合并单元格
        public void MergeCell(int rowMin, int rowMax, int colMin, int colMax)
        {
            Cells cells = sheet.Cells;
            cells.Merge(rowMin, rowMax, colMin, colMax);
        }
    }
}

生成.dll文件后将文件拷贝到cvi工程目录,Tools-》Create .Net Controller选择Assembly by path,然后选择生成Target Instrument名称

    myxls_Class1 Handle;
    myxls_Class1_xlsSet_ xlsSet;
    char name[20] = "123";
    double CurrentDateTime;
    char daytimestring[32]; 
    char xlsSavePath[32];
    switch (event)
    {
        case EVENT_COMMIT:

            GetCurrentDateTime (&CurrentDateTime); 
            FormatDateTimeString (CurrentDateTime, TIME_FORMATSTRING, daytimestring, 32);

            //导入
            CDotNetRegisterAssemblyPath(DLL_Name,"C:\\Users\\Administrator\\Desktop\\多线程测试\\myxls.dll");
            Initialize_myxls();

            myxls_Class1__Create(&Handle, NULL);
            myxls_Class1_CreateNewXLS (Handle, daytimestring, "1", NULL);

            myxls_Class1_xlsSet___Create(&xlsSet, NULL);
            myxls_Class1_xlsSet___Set__startIndex(xlsSet, 0, NULL);
            myxls_Class1_xlsSet___Set__endIndex(xlsSet, 2, NULL);
            myxls_Class1_xlsSet___Set__width(xlsSet, 5000, NULL);
            myxls_Class1_SetXLSStyle(Handle, xlsSet, NULL);
            myxls_Class1_WriteData(Handle, 1,1, "hello", NULL);
            myxls_Class1_WriteData(Handle, 3,3, "hello", NULL);  
            strcpy(xlsSavePath, "D:\\");
            myxls_Class1_SaveXLS(Handle, xlsSavePath, NULL);     
            strcat(xlsSavePath, daytimestring);
            myxls_Class1_OpenXLS(Handle, xlsSavePath, NULL);


            CDotNetDiscardHandle(Handle);

        //  CDotNetFreeMemory(&res);

            Close_myxls();
            break;
    }
    return 0;

猜你喜欢

转载自blog.csdn.net/qq_35439171/article/details/82660696
今日推荐