不使用第三方工具将PDF文件转换为Excel文件 [英] Convert PDF File to Excel File without using third party tool

查看:74
本文介绍了不使用第三方工具将PDF文件转换为Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了更多的资源,但找不到任何资源。我想将pdf表格式转换为excel。我使用iTextSharp但我无法更正输出所有列值合并。因此,PLZ告诉任何来源使用c#

I have search more number of resource but i can't find any one. I want to convert the pdf table formate into excel. I use iTextSharp but i can't correct output all the column values are merged. So plz tell any source to convert pdf table format to excel using c#

推荐答案

将pdf表格式转换为Excel格式嗨...检查此代码。它运行正常。



private void PDFToExcel(DataTable dt)

{

string strPath =D:\\sample.pdf;

string strSplit =;

DataTable dt = new DataTable();

StringBuilder sbTxt = new StringBuilder();



使用(PdfReader pReader = new PdfReader(strPath))

{

for(int i = 1; i< = pReader .NumberOfPages; i ++)

{

strSplit = PdfTextExtractor.GetTextFromPage(pReader,i);

}

}



string [] strSplitArr = strSplit.Split('\ n');

if(strSplitArr.Length> 0)

{

string [] strCol = strSplitArr [0] .Split('');

foreach(strCol中的var col)

{

dt.Columns.Add(col);

}



for(int i = 1;我< strSplitArr.Length; i ++)

{

string [] strRow = strSplitArr [i] .Split('');

DataRow dr = dt.NewRow( );



for(int j = 0; j< strCol.Length; j ++)

{

dr [strCol [j] .ToString()] = strRow [j] .ToString();

}

dt.Rows.Add(dr);

}



DatatableToExcel(dt);

}

}





private void DatatableToExcel(DataTable dt)

{



GridView GridView1 = new GridView();

GridView1.AllowPaging = false;

GridView1.DataSource = dt;

GridView1.DataBind();



Response.Clear( );

Response.Buffer = true;

Response.AddHeader(content-disposition,attachment; filename = MyPdfExcel.xls);

Response.Charset =;

Response.ContentType =application / vnd.ms-excel;

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);





for(int i = 0; i< GridView1 .Rows.Count; i ++)



{

GridView1.Rows [i] .Attributes.Add(class,textmode);



}



GridView1.RenderControl(hw);

string style = @。textmode {mso-number-format:\ @;};

Response.Write(样式);

Response.Output.Write(sw.ToString());

Response.Flush();

Response.End();

}
Hi...check this code.It's working perfectly.

private void PDFToExcel(DataTable dt)
{
string strPath = "D:\\sample.pdf";
string strSplit="";
DataTable dt = new DataTable();
StringBuilder sbTxt = new StringBuilder();

using (PdfReader pReader = new PdfReader(strPath))
{
for (int i = 1; i <= pReader.NumberOfPages; i++)
{
strSplit = PdfTextExtractor.GetTextFromPage(pReader, i);
}
}

string[] strSplitArr = strSplit.Split('\n');
if (strSplitArr.Length > 0)
{
string[] strCol = strSplitArr[0].Split(' ');
foreach (var col in strCol)
{
dt.Columns.Add(col);
}

for (int i = 1; i < strSplitArr.Length; i++)
{
string[] strRow = strSplitArr[i].Split(' ');
DataRow dr = dt.NewRow();

for (int j = 0; j < strCol.Length; j++)
{
dr[strCol[j].ToString()] = strRow[j].ToString();
}
dt.Rows.Add(dr);
}

DatatableToExcel(dt);
}
}


private void DatatableToExcel(DataTable dt)
{

GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=MyPdfExcel.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);


for (int i = 0; i < GridView1.Rows.Count; i++)

{
GridView1.Rows[i].Attributes.Add("class", "textmode");

}

GridView1.RenderControl(hw);
string style = @" .textmode { mso-number-format:\@; } ";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}


这篇关于不使用第三方工具将PDF文件转换为Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆