Jmeter modify the content of the excel file

Recently I am preparing for a performance test. There is an interface to upload some information, including mobile phone number. But the mobile phone number cannot be repeated, so I want to use it to modify the mobile phone number in real time during runtime. So I wrote a method to do it.

In fact, two columns need to be modified here, so I was lazy. The last 8 digits are the same and modified together.

The code is relatively simple:

package Yq;

import jxl. *;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write. *;
import java.io.File;    
import java.util.Random;

public class UpdateExcel {

     public void update_Exc(String filepath) {
           //File f=new File("D:\\work\\Resources\\demos\\insuranceDemo.xls");
            File f=new File(filepath);
            try {
            Workbook wb = Workbook.getWorkbook(f);//
            WritableWorkbook book = wb.createWorkbook(f, wb);

            // Sheet sheet = wb.getSheet(0); // Get the first worksheet object
            WritableSheet st = book.getSheet(0);
            for (int i = 2; i <st.getRows(); i++) {

                StringBuilder str1=new StringBuilder();
                for(int num=0; num<8; num++)
                {                     if(num==0)                         str1.append(new Random().nextInt(9)+1);                     else                         str1.append (new Random().nextInt(10));                 }                 //Modify the third column of data                 Cell cel = st.getCell(2,i);                 if(cel.getType() == CellType.LABEL){                     Label label = new Label(2, i, str1.toString());                     st.addCell(label);                 }                 //Modify the sixth column of data                 Cell cel2=st.getCell(5, i);











                


                if(cel2.getType() == CellType.LABEL){                     Label label = new Label(5, i, "199"+str1.toString());                     // Fill the cell with the phone number                     st.addCell(label) ;                 }             }             book.write();             book.close();             wb.close();             } catch (Exception e) {                 e.printStackTrace();             }         }




                







}
Jxl.jar is a third-party jar package and needs to be downloaded. After downloading, refer to the project.

Export the project to the jar package and add it to jmeter

Call the method in the BeanShell Sampler in jmeter. You may encounter some problems during the call, you can check the previous article, I hope it helps. https://blog.csdn.net/tianyueWindbg/article/details/113870747

import Yq.UpdateExcel; //Project package name. Class name

UpdateExcel updateExc = new UpdateExcel();
updateExc.update_Exc("D:\\work\\Resources\\demos\\insuranceDemo.xls");

As for jmeter upload files, you can refer to another article: https://blog.csdn.net/tianyueWindbg/article/details/113774528

 

 

Guess you like

Origin blog.csdn.net/tianyueWindbg/article/details/113890346