Excel de Bir Aralıktaki Verileri Arama ve Değiştirme
Aspose.Cells, veri arama işlemi için bir aralığı belirtmek için FindOptions.setRange() yöntemini sağlar.
Varsayalım ki “arama” dizesini “değiştir” ile değiştirmek istiyoruz ve bu işlemi E3:H6 aralığında yapmak istiyoruz. Aşağıdaki ekran görüntüsünde, “arama” dizesi birkaç hücrede görülebilir ancak yalnızca belirli bir aralıkta, burada sarı ile vurgulanmış olan alanda, değiştirmek istiyoruz.
Giriş dosyası
Kodun çalıştırılmasından sonra, çıktı dosyası aşağıdaki gibi görünecektir. Belirtilen aralık içindeki tüm “arama” dizeleri “değiştir” ile değiştirilmiştir.
Çıkış dosyası
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SearchReplaceDataInRange.class); | |
Workbook workbook = new Workbook(dataDir + "input.xlsx"); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Specify the range where you want to search | |
// Here the range is E3:H6 | |
CellArea area = CellArea.createCellArea("E3", "H6"); | |
// Specify Find options | |
FindOptions opts = new FindOptions(); | |
opts.setLookInType(LookInType.VALUES); | |
opts.setLookAtType(LookAtType.ENTIRE_CONTENT); | |
opts.setRange(area); | |
Cell cell = null; | |
do { | |
// Search the cell with value search within range | |
cell = worksheet.getCells().find("search", cell, opts); | |
// If no such cell found, then break the loop | |
if (cell == null) | |
break; | |
// Replace the cell with value replace | |
cell.putValue("replace"); | |
} while (true); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsx"); |