1.golang读åè¶
大excelï¼
2.C#主è¦å¼åä»ä¹ç±»åç软件
3.我把程序修改了 不知道怎样在ws程序下重读脚本
golang读åè¶ å¤§excelï¼
Golang-tealeg/xlsxæä½excelæ件
åè§?源码
å表æ¶å¨å¾ªç¯ä¸çcell
åºè¯¥æ°å£°æçåévarcell1*xlsx.Cellå§
ç¶åä¸é¢ç¨cell1åéåå¼
ä¸ç¶å¥½ååä¸è¿å»
使ç¨çæ¹æ³ï¼sheet.SetColWidth(1,1,.0)
//Setthewidthofarangeofcolumns.
//minå¼å§çåï¼maxç»æçå
//eg:ï¼1,1,ï¼ç¬¬ä¸è¡è®¾ç½®ä¸º
//?ï¼1,3,ï¼ç¬¬ä¸è¡å°ç¬¬ä¸è¡è®¾ç½®ä¸º
//éè¿è®¾ç½®å¤æ¡ï¼è¾¾å°è®¾ç½®å ¨é¨åç宽度çç®ç
func(s*Sheet)SetColWidth(min,maxint,widthfloat)
å¦
golangæä¹éè¿æ°æ®æµç´æ¥çæexcelä¸ä¼ è³ossimportjava.io.*;importjxl.*;â¦â¦â¦â¦try{ //æ建Workbook对象,åªè¯»Workbook对象//ç´æ¥ä»æ¬å°æ件å建Workbook//ä»è¾å ¥æµå建WorkbookInputStreamis=newFileInputStream(sourcefile);jxl.Workbookrwb=Workbook.getWorkbook(is);}catch(Exceptione){ e.printStackTrace();}ä¸æ¦å建äºWorkbookï¼æ们就å¯ä»¥éè¿å®æ¥è®¿é®ExcelSheet(æ¯è¯ï¼å·¥ä½è¡¨)ãåèä¸é¢ç代ç ç段ï¼//è·å第ä¸å¼ Sheet表Sheetrs=rwb.getSheet(0);æ们æ¢å¯è½éè¿Sheetçå称æ¥è®¿é®å®ï¼ä¹å¯ä»¥éè¿ä¸æ æ¥è®¿é®å®ãå¦æéè¿ä¸æ æ¥è®¿é®çè¯ï¼è¦æ³¨æçä¸ç¹æ¯ä¸æ ä»0å¼å§ï¼å°±åæ°ç»ä¸æ ·ãä¸æ¦å¾å°äºSheetï¼æ们就å¯ä»¥éè¿å®æ¥è®¿é®ExcelCell(æ¯è¯ï¼åå æ ¼)ãåèä¸é¢ç代ç ç段ï¼//è·å第ä¸è¡ï¼ç¬¬ä¸åçå¼Cellc=rs.getCell(0,0);Stringstrc=c.getContents();//è·å第ä¸è¡ï¼ç¬¬äºåçå¼Cellc=rs.getCell(1,0);Stringstrc=c.getContents();//è·å第äºè¡ï¼ç¬¬äºåçå¼Cellc=rs.getCell(1,1);Stringstrc=c.getContents();System.out.println("Cell(0,0)"+"value:"+strc+";type:"+c.getType());System.out.println("Cell(1,0)"+"value:"+strc+";type:"+c.getType());System.out.println("Cell(1,1)"+"value:"+strc+";type:"+c.getType());å¦æä» ä» æ¯åå¾Cellçå¼ï¼æ们å¯ä»¥æ¹ä¾¿å°éè¿getContents()æ¹æ³ï¼å®å¯ä»¥å°ä»»ä½ç±»åçCellå¼é½ä½ä¸ºä¸ä¸ªå符串è¿åã示ä¾ä»£ç ä¸Cell(0,0)æ¯ææ¬åï¼Cell(1,0)æ¯æ°ååï¼Cell(1,1)æ¯æ¥æåï¼éè¿getContents()ï¼ä¸ç§ç±»åçè¿åå¼é½æ¯å符åãå¦ææéè¦ç¥éCellå 容çç¡®åç±»åï¼APIä¹æä¾äºä¸ç³»åçæ¹æ³ãåèä¸é¢ç代ç ç段ï¼Stringstrc=null;doublestrc=0.;Datestrc=null;Cellc=rs.getCell(0,0);Cellc=rs.getCell(1,0);Cellc=rs.getCell(1,1);if(c.getType()==CellType.LABEL){ LabelCelllabelc=(LabelCell)c;strc=labelc.getString();}if(c.getType()==CellType.NUMBER){ NmberCellnumc=(NumberCell)c;strc=numc.getValue();}if(c.getType()==CellType.DATE){ DateCelldatec=(DateCell)c;strc=datec.getDate();}System.out.println("Cell(0,0)"+"value:"+strc+";type:"+c.getType());System.out.println("Cell(1,0)"+"value:"+strc+";type:"+c.getType());System.out.println("Cell(1,1)"+"value:"+strc+";type:"+c.getType());å¨å¾å°Cell对象åï¼éè¿getType()æ¹æ³å¯ä»¥è·å¾è¯¥åå æ ¼çç±»åï¼ç¶åä¸APIæä¾çåºæ¬ç±»åç¸å¹é ï¼å¼ºå¶è½¬æ¢æç¸åºçç±»åï¼æåè°ç¨ç¸åºçåå¼æ¹æ³getXXX()ï¼å°±å¯ä»¥å¾å°ç¡®å®ç±»åçå¼ãAPIæä¾äºä»¥ä¸åºæ¬ç±»åï¼ä¸Excelçæ°æ®æ ¼å¼ç¸å¯¹åºï¼å¦ä¸å¾æ示ï¼æ¯ç§ç±»åçå ·ä½æä¹ï¼è¯·åè§JavaExcelAPIDocumentãå½ä½ å®æ对Excelçµåè¡¨æ ¼æ°æ®çå¤çåï¼ä¸å®è¦ä½¿ç¨close()æ¹æ³æ¥å ³éå åå建ç对象ï¼ä»¥éæ¾è¯»åæ°æ®è¡¨çè¿ç¨ä¸æå ç¨çå å空é´ï¼å¨è¯»å大éæ°æ®æ¶æ¾å¾å°¤ä¸ºéè¦ãåèå¦ä¸ä»£ç ç段ï¼//æä½å®ææ¶ï¼å ³é对象ï¼éæ¾å ç¨çå å空é´rwb.close();JavaExcelAPIæä¾äºè®¸å¤è®¿é®Excelæ°æ®è¡¨çæ¹æ³ï¼å¨è¿éæåªç®è¦å°ä»ç»å 个常ç¨çæ¹æ³ï¼å ¶å®çæ¹æ³è¯·åèéå½ä¸çJavaExcelAPIDocumentã?Workbookç±»æä¾çæ¹æ³1.intgetNumberOfSheets()è·å¾å·¥ä½èï¼Workbookï¼ä¸å·¥ä½è¡¨ï¼Sheetï¼ç个æ°ï¼ç¤ºä¾ï¼jxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));intsheets=rwb.getNumberOfSheets();2.Sheet[]getSheets()è¿åå·¥ä½èï¼Workbookï¼ä¸å·¥ä½è¡¨ï¼Sheetï¼å¯¹è±¡æ°ç»ï¼ç¤ºä¾ï¼jxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));Sheet[]sheets=rwb.getSheets();3.StringgetVersion()è¿åæ£å¨ä½¿ç¨çAPIççæ¬å·ï¼å¥½åæ¯æ²¡ä»ä¹å¤ªå¤§çä½ç¨ãjxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));StringapiVersion=rwb.getVersion();?Sheetæ¥å£æä¾çæ¹æ³1.StringgetName()è·åSheetçå称ï¼ç¤ºä¾ï¼jxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));jxl.Sheetrs=rwb.getSheet(0);StringsheetName=rs.getName();2.intgetColumns()è·åSheet表ä¸æå å«çæ»åæ°ï¼ç¤ºä¾ï¼jxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));jxl.Sheetrs=rwb.getSheet(0);intrsColumns=rs.getColumns();3.Cell[]getColumn(intcolumn)è·åæä¸åçææåå æ ¼ï¼è¿åçæ¯åå æ ¼å¯¹è±¡æ°ç»ï¼ç¤ºä¾ï¼jxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));jxl.Sheetrs=rwb.getSheet(0);Cell[]cell=rs.getColumn(0);4.intgetRows()è·åSheet表ä¸æå å«çæ»è¡æ°ï¼ç¤ºä¾ï¼jxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));jxl.Sheetrs=rwb.getSheet(0);intrsRows=rs.getRows();5.Cell[]getRow(introw)è·åæä¸è¡çææåå æ ¼ï¼è¿åçæ¯åå æ ¼å¯¹è±¡æ°ç»ï¼ç¤ºä¾åï¼jxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));jxl.Sheetrs=rwb.getSheet(0);Cell[]cell=rs.getRow(0);6.CellgetCell(intcolumn,introw)è·åæå®åå æ ¼ç对象å¼ç¨ï¼éè¦æ³¨æçæ¯å®ç两个åæ°ï¼ç¬¬ä¸ä¸ªæ¯åæ°ï¼ç¬¬äºä¸ªæ¯è¡æ°ï¼è¿ä¸é常çè¡ãåç»åæäºä¸åãjxl.Workbookrwb=jxl.Workbook.getWorkbook(newFile(sourcefile));jxl.Sheetrs=rwb.getSheet(0);Cellcell=rs.getCell(0,0);çææ°çExcelå·¥ä½èä¸é¢ç代ç 主è¦æ¯å大家ä»ç»å¦ä½çæç®åçExcelå·¥ä½è¡¨ï¼å¨è¿éåå æ ¼çå 容æ¯ä¸å¸¦ä»»ä½ä¿®é¥°ç(å¦ï¼åä½ï¼é¢è²çç)ï¼ææçå 容é½ä½ä¸ºå符串åå ¥ã(å®æ´ä»£ç è§ExcelWriting.java)ä¸è¯»åExcelå·¥ä½è¡¨ç¸ä¼¼ï¼é¦å è¦ä½¿ç¨Workbookç±»çå·¥åæ¹æ³å建ä¸ä¸ªå¯åå ¥çå·¥ä½è(Workbook)对象ï¼è¿éè¦æ³¨æçæ¯ï¼åªè½éè¿APIæä¾çå·¥åæ¹æ³æ¥å建Workbookï¼èä¸è½ä½¿ç¨WritableWorkbookçæé å½æ°ï¼å 为类WritableWorkbookçæé å½æ°ä¸ºprotectedç±»åã示ä¾ä»£ç ç段å¦ä¸ï¼importjava.io.*;importjxl.*;importjxl.write.*;â¦â¦â¦â¦try{ //æ建Workbook对象,åªè¯»Workbook对象//Method1ï¼å建å¯åå ¥çExcelå·¥ä½èjxl.write.WritableWorkbookwwb=Workbook.createWorkbook(newFile(targetfile));//Method2ï¼å°WritableWorkbookç´æ¥åå ¥å°è¾åºæµ/*OutputStreamos=newFileOutputStream(targetfile);jxl.write.WritableWorkbookwwb=Workbook.createWorkbook(os);*/}catch(Exceptione){ e.printStackTrace();}APIæä¾äºä¸¤ç§æ¹å¼æ¥å¤çå¯åå ¥çè¾åºæµï¼ä¸ç§æ¯ç´æ¥çææ¬å°æ件ï¼å¦ææ件åä¸å¸¦å ¨è·¯å¾çè¯ï¼ç¼ºççæ件ä¼å®ä½å¨å½åç®å½ï¼å¦ææ件å带æå ¨è·¯å¾çè¯ï¼åçæçExcelæ件åä¼å®ä½å¨ç¸åºçç®å½ï¼å¦å¤ä¸ç§æ¯å°Excel对象ç´æ¥åå ¥å°è¾åºæµï¼ä¾å¦ï¼ç¨æ·éè¿æµè§å¨æ¥è®¿é®Webæå¡å¨ï¼å¦æHTTP头设置æ£ç¡®çè¯ï¼æµè§å¨èªå¨è°ç¨å®¢æ·ç«¯çExcelåºç¨ç¨åºï¼æ¥æ¾ç¤ºå¨æçæçExcelçµåè¡¨æ ¼ãæ¥ä¸æ¥å°±æ¯è¦å建工ä½è¡¨ï¼å建工ä½è¡¨çæ¹æ³ä¸å建工ä½èçæ¹æ³å ä¹ä¸æ ·ï¼åæ ·æ¯éè¿å·¥å模å¼æ¹æ³è·å¾ç¸åºç对象ï¼è¯¥æ¹æ³éè¦ä¸¤ä¸ªåæ°ï¼ä¸ä¸ªæ¯å·¥ä½è¡¨çå称ï¼å¦ä¸ä¸ªæ¯å·¥ä½è¡¨å¨å·¥ä½èä¸çä½ç½®ï¼åèä¸é¢ç代ç ç段ï¼//å建Excelå·¥ä½è¡¨jxl.write.WritableSheetws=wwb.createSheet("TestSheet1",0);"è¿é ä¹æ¯å¥½äºï¼ææä¹åå¤é½å ¨äºï¼å¯ä»¥å¼å§ä¸é äºï¼"ï¼ç°å¨è¦åçåªæ¯å®ä¾åAPIææä¾çExcelåºæ¬æ°æ®ç±»åï¼å¹¶å°å®ä»¬æ·»å å°å·¥ä½è¡¨ä¸å°±å¯ä»¥äºï¼åèä¸é¢ç代ç ç段ï¼//1.æ·»å Label对象jxl.write.LabellabelC=newjxl.write.Label(0,0,"ThisisaLabelcell");ws.addCell(labelC);//æ·»å 带æååFormattingç对象jxl.write.WritableFontwf=newjxl.write.WritableFont(WritableFont.TIMES,,WritableFont.BOLD,true);jxl.write.WritableCellFormatwcfF=newjxl.write.WritableCellFormat(wf);jxl.write.LabellabelCF=newjxl.write.Label(1,0,"ThisisaLabelCell",wcfF);ws.addCell(labelCF);//æ·»å 带æåä½é¢è²Formattingç对象jxl.write.WritableFontwfc=newjxl.write.WritableFont(WritableFont.ARIAL,,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);jxl.write.WritableCellFormatwcfFC=newjxl.write.WritableCellFormat(wfc);jxl.write.LabellabelCFC=newjxl.write.Label(1,0,"ThisisaLabelCell",wcfFC);ws.addCell(labelCF);//2.æ·»å Number对象jxl.write.NumberlabelN=newjxl.write.Number(0,1,3.);ws.addCell(labelN);//æ·»å 带æformattingçNumber对象jxl.write.NumberFormatnf=newjxl.write.NumberFormat("#.##");jxl.write.WritableCellFormatwcfN=newjxl.write.WritableCellFormat(nf);jxl.write.NumberlabelNF=newjxl.write.Number(1,1,3.,wcfN);ws.addCell(labelNF);//3.æ·»å Boolean对象jxl.write.BooleanlabelB=newjxl.write.Boolean(0,2,false);ws.addCell(labelB);//4.æ·»å DateTime对象jxl.write.DateTimelabelDT=newjxl.write.DateTime(0,3,newjava.util.Date());ws.addCell(labelDT);//æ·»å 带æformattingçDateFormat对象jxl.write.DateFormatdf=newjxl.write.DateFormat("ddMMyyyyhh:mm:ss");jxl.write.WritableCellFormatwcfDF=newjxl.write.WritableCellFormat(df);jxl.write.DateTimelabelDTF=newjxl.write.DateTime(1,3,newjava.util.Date(),wcfDF);ws.addCell(labelDTF);è¿éæ两ç¹å¤§å®¶è¦å¼èµ·å¤§å®¶ç注æã第ä¸ç¹ï¼å¨æé åå æ ¼æ¶ï¼åå æ ¼å¨å·¥ä½è¡¨ä¸çä½ç½®å°±å·²ç»ç¡®å®äºãä¸æ¦å建åï¼åå æ ¼çä½ç½®æ¯ä¸è½å¤åæ´çï¼å°½ç®¡åå æ ¼çå 容æ¯å¯ä»¥æ¹åçã第äºç¹ï¼åå æ ¼çå®ä½æ¯æç §ä¸é¢è¿æ ·çè§å¾(column,row)ï¼èä¸ä¸æ é½æ¯ä»0å¼å§ï¼ä¾å¦ï¼A1被åå¨å¨(0,0)ï¼B1被åå¨å¨(1,0)ãæåï¼ä¸è¦å¿è®°å ³éæå¼çExcelå·¥ä½è对象ï¼ä»¥éæ¾å ç¨çå åï¼åè§ä¸é¢ç代ç ç段ï¼//åå ¥Exelå·¥ä½è¡¨wwb.write();//å ³éExcelå·¥ä½è对象wwb.close();è¿å¯è½ä¸è¯»åExcelæ件çæä½æå°å°ä¸åï¼å¨å ³éExcel对象ä¹åï¼ä½ å¿ é¡»è¦å è°ç¨write()æ¹æ³ï¼å 为å åçæä½é½æ¯åå¨å¨ç¼åä¸çï¼æ以è¦éè¿è¯¥æ¹æ³å°æä½çå 容ä¿åå¨æ件ä¸ãå¦æä½ å å ³éäºExcel对象ï¼é£ä¹åªè½å¾å°ä¸å¼ 空çå·¥ä½èäºãæ·è´ãæ´æ°Excelå·¥ä½èæ¥ä¸æ¥ç®è¦ä»ç»ä¸ä¸å¦ä½æ´æ°ä¸ä¸ªå·²ç»åå¨çå·¥ä½èï¼ä¸»è¦æ¯ä¸é¢äºæ¥æä½ï¼ç¬¬ä¸æ¥æ¯æé åªè¯»çExcelå·¥ä½èï¼ç¬¬äºæ¥æ¯å©ç¨å·²ç»å建çExcelå·¥ä½èå建æ°çå¯åå ¥çExcelå·¥ä½èï¼åèä¸é¢ç代ç ç段ï¼(å®æ´ä»£ç è§ExcelModifying.java)//å建åªè¯»çExcelå·¥ä½èç对象jxl.Workbookrw=jxl.Workbook.getWorkbook(newFile(sourcefile));//å建å¯åå ¥çExcelå·¥ä½è对象jxl.write.WritableWorkbookwwb=Workbook.createWorkbook(newFile(targetfile),rw);//读å第ä¸å¼ å·¥ä½è¡¨jxl.write.WritableSheetws=wwb.getSheet(0);//è·å¾ç¬¬ä¸ä¸ªåå æ ¼å¯¹è±¡jxl.write.WritableCellwc=ws.getWritableCell(0,0);//å¤æåå æ ¼çç±»å,ååºç¸åºç转åif(wc.getType()==CellType.LABEL){ Labell=(Label)wc;l.setString("Thevaluehasbeenmodified.");}//åå ¥Excel对象wwb.write();//å ³éå¯åå ¥çExcel对象wwb.close();//å ³éåªè¯»çExcel对象rw.close();ä¹æ以使ç¨è¿ç§æ¹å¼æ建Excel对象ï¼å®å ¨æ¯å 为æççåå ï¼å 为ä¸é¢ç示ä¾ææ¯APIç主è¦åºç¨ã为äºæé«æ§è½ï¼å¨è¯»åå·¥ä½è¡¨æ¶ï¼ä¸æ°æ®ç¸å ³çä¸äºè¾åºä¿¡æ¯ï¼ææçæ ¼å¼ä¿¡æ¯ï¼å¦ï¼åä½ãé¢è²ççï¼æ¯ä¸è¢«å¤ççï¼å 为æ们çç®çæ¯è·å¾è¡æ°æ®çå¼ï¼æ¢ä½¿æ²¡æäºä¿®é¥°ï¼ä¹ä¸ä¼å¯¹è¡æ°æ®çå¼äº§çä»ä¹å½±åãå¯ä¸çä¸å©ä¹å¤å°±æ¯ï¼å¨å åä¸ä¼åæ¶ä¿å两个åæ ·çå·¥ä½è¡¨ï¼è¿æ ·å½å·¥ä½è¡¨ä½ç§¯æ¯è¾å¤§æ¶ï¼ä¼å ç¨ç¸å½å¤§çå åï¼ä½ç°å¨å¥½åå åç大å°å¹¶ä¸æ¯ä»ä¹å ³é®å ç´ äºãä¸æ¦è·å¾äºå¯åå ¥çå·¥ä½è¡¨å¯¹è±¡ï¼æ们就å¯ä»¥å¯¹åå æ ¼å¯¹è±¡è¿è¡æ´æ°çæä½äºï¼å¨è¿éæ们ä¸å¿ è°ç¨APIæä¾çadd()æ¹æ³ï¼å 为åå æ ¼å·²ç»äºå·¥ä½è¡¨å½ä¸ï¼æ以æ们åªéè¦è°ç¨ç¸åºçsetXXX()æ¹æ³ï¼å°±å¯ä»¥å®ææ´æ°çæä½äºãå°½åå æ ¼åæçæ ¼å¼å修饰æ¯ä¸è½å»æçï¼æ们è¿æ¯å¯ä»¥å°æ°çåå æ ¼ä¿®é¥°å ä¸å»ï¼ä»¥ä½¿åå æ ¼çå 容以ä¸åçå½¢å¼è¡¨ç°ãæ°çæçå·¥ä½è¡¨å¯¹è±¡æ¯å¯åå ¥çï¼æ们é¤äºæ´æ°åæçåå æ ¼å¤ï¼è¿å¯ä»¥æ·»å æ°çåå æ ¼å°å·¥ä½è¡¨ä¸ï¼è¿ä¸ç¤ºä¾2çæä½æ¯å®å ¨ä¸æ ·çãæåï¼ä¸è¦å¿è®°è°ç¨write()æ¹æ³ï¼å°æ´æ°çå 容åå ¥å°æ件ä¸ï¼ç¶åå ³éå·¥ä½è对象ï¼è¿éæ两个工ä½è对象è¦å ³éï¼ä¸ä¸ªæ¯åªè¯»çï¼å¦å¤ä¸ä¸ªæ¯å¯åå ¥çãå°ç»æ¬æåªæ¯å¯¹JavaExcelAPIä¸å¸¸ç¨çæ¹æ³ä½äºä»ç»ï¼è¦æ³æ´è¯¦å°½å°äºè§£APIï¼è¯·å¤§å®¶åèAPIææ¡£ï¼ææºä»£ç ãJavaExcelAPIæ¯ä¸ä¸ªå¼æ¾æºç 项ç®ï¼è¯·å¤§å®¶å ³æ³¨å®çææ°è¿å±ï¼æå ´è¶£çæåä¹å¯ä»¥ç³è¯·å å ¥è¿ä¸ªé¡¹ç®ï¼æè æ¯æåºå®è´µçæè§ã
Golangæä½xlsxæ件github.com/cuishu/excelæ¯ç¨æ¥è¾ å©æä½xlsxæ件çåºï¼å®ç°äºxlsxæ件ågo对象çæ å°ï¼ä½¿æä½xlsxæ件å¦åæä½go对象ä¸æ ·ç®åã
ä¾èµgithub.com/EntSecGroup-Skylar/excelize/v2
excelæ件ç第ä¸è¡å¿ é¡»åGostructçtagä¸è´
Forexample
æ°å»ºä¸ä¸ªæ件ï¼å为a.xlsx
Sheet1ç»æå¦ä¸
读åSheet
å°gosliceåå ¥excelæ件
æ¯æçæ°æ®ç±»å
å¦ææ件æä¸æ¢ä¸ä¸ªSheetï¼åºè¯¥ä½¿ç¨ä¸ä¸ªç»ææ¥æ å°å®ä»¬ã
ä¾
æ¨å¯ä»¥ä½¿ç¨èªå®ä¹ç±»åæ¥å®ç°MarshalXLSXåUnmarshalXLSXæ¥å®ç°ç±»å转æ¢ã
ä¾
C#主è¦å¼åä»ä¹ç±»åç软件
å¯ä»¥å¼å以ä¸ç±»åçç¨åºï¼1ãWindowsæ¡é¢åºç¨ç¨åºï¼ç¨.Netæä¾çWPF模åå¾ä¾¿æ·çå°±è½çææ¡é¢åºç¨ã
2ãWindows Storeåºç¨ç¨åºï¼è¿æ¯Win8以ä¸ç³»ç»é对触æ¸å±åºç设计ï¼éå¸¸å ¨å±è¿è¡çã
3ãWebåºç¨ç¨åºï¼éè¿webFormså建çASP.NETåºç¨ç¨åºã
4ãWCFæå¡ï¼æ¯è¾é«çº§ï¼æ¯ä¸ç§çµæ´»å建åç§åå¸å¼åºç¨ç¨åºçæ¹å¼ï¼å¯éè¿å±åç½æäºèç½äº¤äºå ä¹åç§æ°æ®ã
æ©å±èµæ
ç¨åºæ§è¡
C#æå¼åçç¨åºæºä»£ç 并ä¸æ¯ç¼è¯æè½å¤ç´æ¥å¨æä½ç³»ç»ä¸æ§è¡çäºè¿å¶æ¬å°ä»£ç ãä¸Java类似ï¼å®è¢«ç¼è¯æ为ä¸é´ä»£ç ï¼ç¶åéè¿.NETFrameworkçèææºââ被称ä¹ä¸ºéç¨è¯è¨è¿è¡åºï¼CLRï¼ââæ§è¡ã
ææç.Netç¼ç¨è¯è¨é½è¢«ç¼è¯æè¿ç§è¢«ç§°ä¸ºMSILï¼Microsoft Intermediate Language ï¼çä¸é´ä»£ç ãå æ¤è½ç¶æç»çç¨åºå¨è¡¨é¢ä¸ä»ç¶ä¸ä¼ ç»æä¹ä¸çå¯æ§è¡æ件é½å ·æâ.exeâçåç¼åãä½æ¯å®é ä¸ï¼å¦æ计ç®æºä¸æ²¡æå®è£ .Net Frameworkï¼é£ä¹è¿äºç¨åºå°ä¸è½å¤è¢«æ§è¡ã
å¨ç¨åºæ§è¡æ¶ï¼.Net Frameworkå°ä¸é´ä»£ç ç¿»è¯æ为äºè¿å¶æºå¨ç ï¼ä»è使å®å¾å°æ£ç¡®çè¿è¡ãæç»çäºè¿å¶ä»£ç 被åå¨å¨ä¸ä¸ªç¼å²åºä¸ãæ以ä¸æ¦ç¨åºä½¿ç¨äºç¸åç代ç ï¼é£ä¹å°ä¼è°ç¨ç¼å²åºä¸ççæ¬ãè¿æ ·å¦æä¸ä¸ª.Netç¨åºç¬¬äºæ¬¡è¢«è¿è¡ï¼é£ä¹è¿ç§ç¿»è¯ä¸éè¦è¿è¡ç¬¬äºæ¬¡ï¼é度ææ¾å å¿«ã
åèèµææ¥æºï¼ç¾åº¦ç¾ç§-c#
我把程序修改了 不知道怎样在ws程序下重读脚本
首先,动态编译实际上只涉及到两个类型:CodeDomProvider以及CompilerParameters他们都位于System.CodeDom.Compiler命名空间。源码
以下代码可将源码动态编译为一个程序集:
动态编译
获得assembly后,源码随后我们即可以通过反射获取程序集里面的源码网站源码优化类型,然后实例化,源码调用类型方法
不过在此之前,源码我们得构造WS服务的源码代理类,使用WCF框架,源码创建服务代理类,源码常见的源码代理类结构如下:
所以,我们要动态构造出代理类源码,源码qq华夏贴吧火山源码应该知道服务的源码命名空间、服务方法的源码Action地址、ReplyAction地址,源码当然还有服务方法的源码名称,返回类型,跑步机中的源码参数列表。这里,我们省略掉服务方法的参数列表,构造代理类,实际上就是通达信大牛底部源码一个字符串组装的问题,先创建一个类型,用于保存构造代理类所要用到的参数:
服务代理类构造参数
public class WebServiceParamaters
{
public string address;
public string Address
{
get { return address; }
set
{
address = value;
}
}
private string serviceNamespace;
public string ServiceNamespace
{
get { return serviceNamespace; }
set
{
serviceNamespace = value;
}
}
private string methodAction;
public string MethodAction
{
get { return methodAction; }
set
{
methodAction = value;
}
}
private string methodReplyAction;
public string MethodReplyAction
{
get { return methodReplyAction; }
set
{
methodReplyAction = value;
}
}
private string methodName;
public string MethodName
{
get { return methodName; }
set
{
methodName = value;
}
}
private string returnType;
public string ReturnType
{
get { return returnType; }
set
{
returnType = value;
}
}
}
好,现在我们只需要构造出代理类源码,然后动态编译出代理类的程序集,最后通过反射调用服务方法:
WebServiceProxyCreator
public class WebServiceProxyCreator
{
public Object WebServiceCaller(WebServiceParamaters parameters)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters codeParameters = new CompilerParameters();
codeParameters.GenerateExecutable = false;
codeParameters.GenerateInMemory = true;
StringBuilder code = new StringBuilder();
CreateProxyCode(code,福建会议直播系统源码错误 parameters);
codeParameters.ReferencedAssemblies.Add("System.dll");
codeParameters.ReferencedAssemblies.Add("System.ServiceModel.dll");
CompilerResults results = provider.CompileAssemblyFromSource(codeParameters, code.ToString());
Assembly assembly = null;
if (!results.Errors.HasErrors)
{
assembly = results.CompiledAssembly;
}
Type clientType = assembly.GetType("RuntimeServiceClient");
ConstructorInfo ci = clientType.GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
BasicHttpBinding binding = new BasicHttpBinding(); //只演示传统的WebService调用
EndpointAddress address = new EndpointAddress(parameters.address);
Object client = ci.Invoke(new object[] { binding, address });
MethodInfo mi = clientType.GetMethod(parameters.MethodName);
Object result = mi.Invoke(client, null);
mi = clientType.GetMethod("Close"); //关闭代理
mi.Invoke(client, null);
return result;
}
public static void CreateProxyCode(StringBuilder code, WebServiceParamaters parameters)
{
code.AppendLine("using System;");
code.AppendLine("using System.ServiceModel;");
code.AppendLine("using System.ServiceModel.Channels;");
code.Append(@"[ServiceContract(");
if (!String.IsNullOrEmpty(parameters.ServiceNamespace))
{
code.Append("Namespace=\"").Append(parameters.ServiceNamespace).Append("\"");
}
code.AppendLine(")]");
code.AppendLine("public interface IRuntimeService");
code.AppendLine("{ ");
code.Append("[OperationContract(");
if (!String.IsNullOrEmpty(parameters.MethodAction))
{
code.Append("Action=\"").Append(parameters.MethodAction).Append("\"");
if (!String.IsNullOrEmpty(parameters.MethodReplyAction))
{
code.Append(", ");
}
}
if (!String.IsNullOrEmpty(parameters.MethodReplyAction))
{
code.Append("ReplyAction=\"").Append(parameters.MethodReplyAction).Append("\"");
}
code.AppendLine(")]");
code.Append(parameters.ReturnType).Append(" ");
code.Append(parameters.MethodName).AppendLine("();");
code.AppendLine("}");
code.AppendLine();
code.AppendLine("public class RuntimeServiceClient : ClientBase<IRuntimeService>, IRuntimeService");
code.AppendLine("{ ");
code.AppendLine("public RuntimeServiceClient(Binding binding, EndpointAddress address) :base(binding, address)");
code.AppendLine("{ ");
code.AppendLine("}");
code.Append("public ").Append(parameters.ReturnType).Append(" ");
code.Append(parameters.MethodName).AppendLine("()");
code.AppendLine("{ ");
code.Append("return base.Channel.").Append(parameters.MethodName).AppendLine("();");
code.AppendLine("}");
code.AppendLine("}");
}
}
注意,由于代理类使用了WCF框架,所以编译时我们需要添加System.ServiceModel的引用,当然System.dll肯定是必须的,这里要注意,System.ServiceModel.dll应该保存到应用程序目录,否则动态编译时会引发异常,很简单,在工程引用中添加System.ServiceModel的引用,然后在属性中将拷贝到本地属性设置为true。
到此,我们就可以直接通过传入的服务地址、服务方法名称以及相关的命名空间,即可调用服务(尽管我们只能调用无参服务,并且尽管我们也只能调用使用BasicHttpBinding绑定的服务,这些限制的原因是…我懒,好吧,相信只要经过一点改动即可去掉这些限制)。
可惜,我们的程序还很傻:每次调用服务都需要去生成代码、编译、创建代理实例最后再调用,嗯…那就缓存吧:
在WebServiceParameters类中重写GetHashCode方法:
然后在WebServiceProxyCreator中加入缓存机制:
public class WebServiceProxyCreator
{
private static Dictionary<int, Type> proxyTypeCatch = new Dictionary<int, Type>();
public Object WebServiceCaller(WebServiceParamaters parameters)
{
int key = parameters.GetHashCode();
Type clientType = null;
if (proxyTypeCatch.ContainsKey(key))
{
clientType = proxyTypeCatch[key];
Debug.WriteLine("使用缓存");
}
else
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters codeParameters = new CompilerParameters();
codeParameters.GenerateExecutable = false;
codeParameters.GenerateInMemory = true;
StringBuilder code = new StringBuilder();
CreateProxyCode(code, parameters);
codeParameters.ReferencedAssemblies.Add("System.dll");
codeParameters.ReferencedAssemblies.Add("System.ServiceModel.dll");
CompilerResults results = provider.CompileAssemblyFromSource(codeParameters, code.ToString());
Assembly assembly = null;
if (!results.Errors.HasErrors)
{
assembly = results.CompiledAssembly;
}
clientType = assembly.GetType("RuntimeServiceClient");
proxyTypeCatch.Add(key, clientType);
}
ConstructorInfo ci = clientType.GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
BasicHttpBinding binding = new BasicHttpBinding(); //只演示传统的WebService调用
EndpointAddress address = new EndpointAddress(parameters.address);
Object client = ci.Invoke(new object[] { binding, address });
MethodInfo mi = clientType.GetMethod(parameters.MethodName);
Object result = mi.Invoke(client, null);
mi = clientType.GetMethod("Close"); //关闭代理
mi.Invoke(client, null);
return result;
}
}
2024-12-22 15:45
2024-12-22 14:40
2024-12-22 14:28
2024-12-22 14:06
2024-12-22 13:49
2024-12-22 13:28