StreamProvider ile HTML yü Kaydet
Resimler ve şekiller içeren excel dosyalarının HTML dosyalarına dönüştürülmesi sırasında genellikle aşağıdaki iki sorunla karşılaşırız:
- Excel dosyasını html olarak kaydederken görüntü ve şekilleri nereye kaydedeceğiz.
- Varsayılan yolu beklenen yol ile değiştirin.
Bu makale, HtmlSaveOptions.StreamProvider özelliğini ayarlamak için IStreamProvider arayüzünü uygulamanın nasıl başarılacağını açıklar. Bu arayüzü uygulayarak, HTML oluşturma sırasında oluşturulan kaynakları belirli konumlara veya bellek akışlarına kaydedebileceksiniz.
Örnek Kod
Bu, HtmlSaveOptions.StreamProvider özelliğinin kullanımını gösteren ana kod
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(HtmlSaveOptions.class); | |
Workbook wb = new Workbook(dataDir + "sample.xlsx"); | |
HtmlSaveOptions options = new HtmlSaveOptions(); | |
options.setStreamProvider(new ExportStreamProvider(dataDir)); | |
wb.save(dataDir + "out.html", options); |
Yukarıdaki kodun içinde kullanılan IStreamProvider arayüzünü uygulayan ExportStreamProvider sınıfının kodu aşağıda bulunmaktadır.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public class ExportStreamProvider implements IStreamProvider { | |
private String outputDir; | |
public ExportStreamProvider(String dir) { | |
outputDir = dir; | |
System.out.println(outputDir); | |
} | |
@Override | |
public void closeStream(StreamProviderOptions options) throws Exception { | |
if (options != null && options.getStream() != null) { | |
options.getStream().close(); | |
} | |
} | |
@Override | |
public void initStream(StreamProviderOptions options) throws Exception { | |
System.out.println(options.getDefaultPath()); | |
File file = new File(outputDir); | |
if (!file.exists() && !file.isDirectory()) { | |
file.mkdirs(); | |
} | |
String defaultPath = options.getDefaultPath(); | |
String path = outputDir + defaultPath.substring(defaultPath.lastIndexOf("/") + 1); | |
options.setCustomPath(path); | |
options.setStream(new FileOutputStream(path)); | |
} | |
} | |