Ši trumpa pamoka išmokys jus kaip sukurti duomenų patvirtinimą programoje Excel naudojant Java. Jame pateikiami ištekliai aplinkai sukurti, veiksmų, kurių reikia atlikti norint atlikti užduotį, sąrašas ir vykdomas pavyzdinis kodas, skirtas įterpti duomenų patvirtinimą programoje Excel naudojant Java. Gausite kodo aprašymą ir galiausiai sukursite XLS arba XLSX failą su patvirtinimo taisykle.
Veiksmai, kaip sukurti duomenų patvirtinimo taisyklę programoje „Excel“ naudojant „Java“.
- Sukurkite aplinką, kad pridėtumėte Aspose.Cells for Java, kad įterptumėte patvirtinimą
- Sukurkite naują workbook ir pridėkite worksheet, kad prie jo pridėtumėte nuorodos duomenis
- Sukurkite diapazono klasės objektą ir nustatykite jo pavadinimą bei pageidaujamą reikšmių sąrašą
- Sukurkite naują patvirtinimą tikslinio lapo patvirtinimų rinkinyje ir nustatykite jo langelio sritį
- Nustatykite kitas patvirtinimo savybes
- Išsaugokite darbaknygę diske su patvirtinimu
Šie veiksmai paaiškina kaip įtraukti duomenų patvirtinimą programoje Excel naudojant Java procesą. Turite sukurti darbaknygę, sukurti joje darbalapį, kad pridėtumėte sąrašo duomenis, sukurtumėte diapazono klasės objektą ir nustatytumėte jį naujai sukurtame patvirtinime. Galiausiai nustatykite skirtingas patvirtinimo ypatybes ir išsaugokite gautą darbaknygę diske.
Kodas, skirtas sukurti duomenų patvirtinimo sąrašą programoje „Excel“ naudojant „Java“.
import com.aspose.cells.CellArea; | |
import com.aspose.cells.License; | |
import com.aspose.cells.OperatorType; | |
import com.aspose.cells.Range; | |
import com.aspose.cells.Validation; | |
import com.aspose.cells.ValidationAlertType; | |
import com.aspose.cells.ValidationCollection; | |
import com.aspose.cells.ValidationType; | |
import com.aspose.cells.Workbook; | |
import com.aspose.cells.Worksheet; | |
public class Main { | |
public static void main(String[] args) throws Exception { // Main function to add validation to a worksheet | |
// Load a license | |
License lic = new License(); | |
lic.setLicense("Aspose.Total.lic"); | |
// Instantiate a workbook | |
Workbook workbook = new Workbook(); | |
// Access the first sheet | |
Worksheet worksheet1 = workbook.getWorksheets().get(0); | |
// Create another sheet for reference data and get access to it | |
int i = workbook.getWorksheets().add(); | |
Worksheet worksheet2 = workbook.getWorksheets().get(i); | |
// Create a range for the reference list | |
Range referenceRange = worksheet2.getCells().createRange("E1", "E4"); | |
// Set the name property of the above-created range | |
referenceRange.setName("ReferenceRange"); | |
// Fill the reference list to be used for validation | |
referenceRange.get(0,0).putValue("Tiny"); | |
referenceRange.get(1, 0).putValue("Small"); | |
referenceRange.get(2, 0).putValue("Medium"); | |
referenceRange.get(3, 0).putValue("Large"); | |
// Get a reference to the validations collection on the first sheet | |
ValidationCollection validations = worksheet1.getValidations(); | |
// Create a cell Area where validation is to be implemented | |
CellArea area = new CellArea(); | |
area.StartRow = 0; | |
area.EndRow = 4; | |
area.StartColumn = 0; | |
area.EndColumn = 0; | |
// Create a new validation for the given cell area defined above | |
validations.add(area); | |
Validation validation = validations.get(validations.add(area)); | |
// Set type of validation | |
validation.setType (ValidationType.LIST); | |
// Set the type of operator | |
validation.setOperator(OperatorType.NONE); | |
// Set flag for in-cell drop-down | |
validation.setInCellDropDown(true); | |
// Set the formula by providing reference data range name | |
validation.setFormula1("=ReferenceRange"); | |
// Enable the flag to show an error | |
validation.setShowError(true); | |
// Set the type of alert on error | |
validation.setAlertStyle(ValidationAlertType.STOP); | |
// Set the title | |
validation.setErrorTitle("Error Title"); | |
// Set the message to be shown when an error is raised | |
validation.setErrorMessage("Please select data from the list"); | |
// Save the output file | |
workbook.save("output.xls"); | |
System.out.println("Done!"); | |
} | |
} |
Šis kodas parodo kaip įterpti duomenų patvirtinimą programoje Excel naudojant Java. Jis naudoja diapazono klasės objektą, kad nustatytų diapazono pavadinimą ir verčių rinkinį, kurį naudos patvirtinimo objektas. Kiekviename darbalapyje yra patvirtinimų rinkinys, kuriame pridedamas naujas patvirtinimas kartu su ypatybėmis ir efektyvia langelio sritimi.
Šioje pamokoje mes sužinojome, kaip įterpti patvirtinimą programoje Excel naudojant Java. Jei norite sužinoti, kaip taikyti Excel filtrus, žr. straipsnį kaip pritaikyti filtrą programoje Excel naudojant Java.