【转】关于document的三个方法:open、write、close

document.open会打开一个新文档,清除已加载的文档;document.write在HTML加载后调用会导致open方法调用一次;document.close会关闭打开的文档。

直接看代码和效果图:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>关于write</title>
</head>
<body>
    <h1>HTML - 1</h1>

    <script>
        function doAsync(fun){
            setTimeout(fun, 3000);
        }
        //调用document.open,清除当前文档,但是需要HTML加载完成后有效
        document.open();
        //文档加载未完成
        //脚本阻塞HTML解析,write会向文档流添加内容
        document.write("<h1>这里调用open、close无效 [文档未加载完成]</h1>");
        document.open();
        document.close();
        document.open();
        document.close();
        doAsync(()=>{
            //文档已加载
            //调用document.write会导致document.open调用一次
            //document.open会清除前文档并开始一个新的文档
            document.write("<h1>文档加载结束。调用open,清除原文档</h1>");
            document.write("<h1>这次没调用open,保持追加</h1>");
            //document.write方法不会导致document.close调用,需要手动调用
            //document.close之后,如果再调用document.write,会导致document.open调用一次
            document.close();

            doAsync(()=>{
                document.open();
                document.write("<h1>这里手动调用open,原文档被清除</h1>");
                //调用document.open会清除页面,即便没有close
                document.open();
                document.write("<h1>这里手动调用open,原文档被清除</h1>");
                document.close();
            });

        });
    </script>

    <h1>HTML - 2</h1>
</body>
</html>
 
 

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java ExcelPDF的工具方法类的示例: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.lowagie.text.Document; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.XfdfReader; public class ExcelToPdfConverter { public static void convert(String inputFile, String outputFile) throws IOException { Workbook workbook = null; if (inputFile.endsWith(".xls")) { workbook = new HSSFWorkbook(); } else if (inputFile.endsWith(".xlsx")) { workbook = new XSSFWorkbook(); } else { throw new IllegalArgumentException("The specified file is not an Excel file"); } // Load Excel file into workbook workbook.setReadOnly(true); workbook.setFileReadOnly(false); workbook.setSaveReadOnly(false); workbook.clearBackupFiles(); workbook.clearTemplateRecords(); workbook.clearPrintAreas(); workbook.clearCellComments(); // Create PDF document Document pdfDocument = new Document(); // Open PDF writer PdfWriter.getInstance(pdfDocument, new FileOutputStream(outputFile)); // Open PDF document pdfDocument.open(); // Convert each sheet in the workbook to a PDF page for (int i = 0; i < workbook.getNumberOfSheets(); i++) { // Get current sheet Sheet sheet = workbook.getSheetAt(i); // Create PDF page for current sheet pdfDocument.newPage(); // Convert sheet to PDF XfdfReader.getInstance(sheet).write(pdfDocument); } // Close PDF document pdfDocument.close(); // Close workbook workbook.close(); } public static void main(String[] args) throws IOException { String inputFile = "test.xlsx"; String outputFile = "test.pdf"; ExcelToPdfConverter.convert(inputFile, outputFile); System.out.println("Conversion complete!"); } } ``` 该示例使用Apache POI和iText库来读取Excel文件并将其换为PDF格式。您需要将这些库添加到您的项目中,然后将输入和输出文件路径传递给convert()方法。请注意,此示例假定您的Excel文件只包含单个工作表。如果您的Excel文件包含多个工作表,则需要更改convert()方法以处理每个工作表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值