页面写成excel文件

博客展示了对Excel数据的Byte流处理代码。通过创建BinaryReader读取请求输入流中的数据到Byte数组,关闭流后,设置响应的编码、类型和文件名,最后将Byte数组写入响应以实现Excel导出,还提到去掉导出页视图状态。

   Stream requestStream ;
   BinaryReader  br;
   byte[] ByteData;
   
   long dataLen = Request.InputStream.Length;

   ByteData = new byte[dataLen];
 
   requestStream = Request.InputStream;
   br = new BinaryReader(requestStream);

   br.Read(ByteData,0,(int)dataLen);

   br.Close();
   requestStream.Close();
   Response.ClearContent();
   Response.ClearHeaders();
   Response.ContentEncoding = System.Text.Encoding.UTF8;
   Response.ContentType = "application/vnd.ms-excel";

  Response.AddHeader("Content-Disposition", "inline/attachment;filename=test.xls");
   Response.BinaryWrite(ByteData);
/////////////////摘要就足够了。同时请去掉导出页的视图状态

### 使用脚本将 PDF 文件转换为 Excel 文件 PDF 文件中的表格数据通常难以直接编辑或分析,因此将其转换为 Excel 文件是常见的需求。Python 提供了多种库来实现这一功能,其中 `camelot` 是一个强大且易于使用的工具。以下介绍几种使用 Python 脚本将 PDF 文件转换为 Excel 文件的方法。 #### 方法一:单页 PDF 转换为 Excel 适用于只需提取 PDF 文件中某一页的表格数据,并将其保存为 Excel 文件的情况。 ```python import camelot.io as camelot # 读取 PDF 文件的第一页 data = camelot.read_pdf("abc.pdf", pages='1', flavor='stream') # 将提取的表格数据保存为 Excel 文件 data[0].to_excel('result.xlsx', encoding='utf_8_sig') ``` 此方法适合处理结构清晰的 PDF 文件,并且仅需提取特定页面的内容[^1]。 #### 方法二:多页 PDF 转换为单个 Excel 文件 当 PDF 文件包含多页表格数据时,可以将所有页面的数据合并到一个 Excel 文件中。 ```python import camelot.io as camelot from openpyxl import Workbook # 创建一个新的 Excel 工作簿 work = Workbook() sheet = work.active # 读取 PDF 文件的所有页面 data = camelot.read_pdf("计算机技术拟录取名单.pdf", pages='all', flavor='stream') # 遍历每一页的表格数据并写入 Excel for i in range(data.n): for rows in data[i].data: sheet.append(rows) # 保存 Excel 文件 work.save('success.xlsx') ``` 此方法适用于需要将多个页面的表格数据整合到一个 Excel 文件中的场景。 #### 方法三:自动化处理多个 PDF 文件 如果需要批量处理多个 PDF 文件,可以使用 `os` 模块遍历文件夹中的所有 PDF 文件,并逐个进行转换。 ```python import camelot.io as camelot import os # 定义 PDF 文件所在的目录 pdf_dir = "path/to/pdf/files" output_dir = "path/to/output/excel" # 遍历目录下的所有 PDF 文件 for filename in os.listdir(pdf_dir): if filename.endswith(".pdf"): pdf_path = os.path.join(pdf_dir, filename) excel_path = os.path.join(output_dir, filename.replace(".pdf", ".xlsx")) # 读取 PDF 文件的所有页面 data = camelot.read_pdf(pdf_path, pages='all', flavor='stream') # 创建一个新的 Excel 工作簿 from openpyxl import Workbook work = Workbook() sheet = work.active # 遍历每一页的表格数据并写入 Excel for i in range(data.n): for rows in data[i].data: sheet.append(rows) # 保存 Excel 文件 work.save(excel_path) ``` 此方法适用于需要处理多个 PDF 文件并自动生成 Excel 文件的情况,适合自动化流程。 #### 方法四:使用 `pdfplumber` 提取表格数据并写入 Excel 除了 `camelot`,还可以使用 `pdfplumber` 提取 PDF 中的表格数据,并结合 `pandas` 写入 Excel 文件。 ```python import pdfplumber import pandas as pd import os # 定义 PDF 文件路径 pdf_path = "abc.pdf" output_excel = "output.xlsx" # 初始化一个空列表用于存储所有表格 all_tables = [] # 打开 PDF 文件 with pdfplumber.open(pdf_path) as pdf: for page in pdf.pages: # 提取当前页面的表格 table = page.extract_table() if table: all_tables.extend(table) # 将提取的表格数据转换为 DataFrame df = pd.DataFrame(all_tables[1:], columns=all_tables[0]) # 保存为 Excel 文件 df.to_excel(output_excel, index=False) ``` 此方法适用于需要更精细控制表格提取逻辑的情况,尤其适合处理复杂格式的 PDF 文件。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值