c#如何将TXT转换成excel格式excel文件格式丢失

没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!拒绝访问 | www.ggdoc.com | 百度云加速
请打开cookies.
此网站 (www.ggdoc.com) 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(14388-ua98).
重新安装浏览器,或使用别的浏览器查看: 35628|回复: 46
C#将数据导出到Excel的各种方法汇总
TA的每日心情开心前天&10:45签到天数: 519 天[LV.9]以坛为家II
说实在的分析了这么多感觉还是不够完美
下面我开一个专题全是我实际使用的给大家分享一下
苏飞分享专区
--------------------------------------------------------------------------------------------------------
1.使用流导入的方法
[C#] 纯文本查看 复制代码
string html = &@006|销售代表||客户代表|&;
string[] list = html.ToLower().Split('|');
string result = &&;
for (int i = 0; i & list.L i++)
if (list.Trim().Contains(&@&) || list.Trim().Contains(&其他&))
result += list.Trim() + &\r\n&;
StreamWriter sw = new StreamWriter(&D:\\abc.xls&, false, System.Text.Encoding.UTF8);
sw.WriteLine(result);
sw.Close();
正在更新中,,,
--------------------------------------------------------------------------------------------------------
一、asp.net中导出Excel的方法:
在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上;一种是将文件直接将文件输出流写给浏览器。在Response输出时,t分隔的数据,导出Excel时,等价于分列,n等价于换行。
1、将整个html全部输出Excel
此法将html中所有的内容,如按钮,表格,图片等全部输出到Excel中。[C#] 纯文本查看 复制代码
Response.Clear();
Response.Buffer=
Response.AppendHeader(&Content-Disposition&,&filename=&+DateTime.Now.ToString(&yyyyMMdd&)+&.xls&);
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType
&application/vnd.ms-excel&;
this.EnableViewState
这里我们利用了ContentType属性,它默认的属性为text/html,这时将输出为超文本,即我们常见的网页格式到客户端,如果改为ms-excel将将输出excel格式,也就是说以电子表格的格式输出到客户端,这时浏览器将提示你下载保存。ContentType的属性还包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我们也可以输出(导出)图片、word文档等。下面的方法,也均用了这个属性。
2、将DataGrid控件中的数据导出Excel
上述方法虽然实现了导出的功能,但同时把按钮、分页框等html中的所有输出信息导了进去。而我们一般要导出的是数据,DataGrid控件上的数据。[C#] 纯文本查看 复制代码
System.Web.UI.Control ctl=this.DataGrid1;
//DataGrid1是你在窗体中拖放的控件
HttpContext.Current.Response.AppendHeader(&Content-Disposition&,&filename=Excel.xls&);
HttpContext.Current.Response.Charset =&UTF-8&;
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.D
HttpContext.Current.Response.ContentType =&application/ms-excel&;
ctl.Page.EnableViewState =
System.IO.StringWriter
tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
如果你的DataGrid用了分页,它导出的是当前页的信息,也就是它导出的是DataGrid中显示的信息。而不是你select语句的全部信息。
为方便使用,写成方法如下:
[C#] 纯文本查看 复制代码
public void DGToExcel(System.Web.UI.Control ctl)
HttpContext.Current.Response.AppendHeader(&Content-Disposition&,&filename=Excel.xls&);
HttpContext.Current.Response.Charset =&UTF-8&;
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.D
HttpContext.Current.Response.ContentType =&application/ms-excel&;
ctl.Page.EnableViewState =
System.IO.StringWriter
tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}用法:DGToExcel(datagrid1);
3、将DataSet中的数据导出Excel
有了上边的思路,就是将在导出的信息,输出(Response)客户端,这样就可以导出了。那么把DataSet中的数据导出,也就是把DataSet中的表中的各行信息,以ms-excel的格式Response到http流,这样就OK了。说明:参数ds应为填充有数据表的DataSet,文件名是全名,包括后缀名,如Excel2006.xls
[C#] 纯文本查看 复制代码 public
void CreateExcel(DataSet ds,string FileName)
resp = Page.R
resp.ContentEncoding = System.Text.Encoding.GetEncoding(&GB2312&);
resp.AppendHeader(&Content-Disposition&, &filename=&+FileName);
string colHeaders= &&, ls_item=&&; //定义表对象与行对象,同时用DataSet对其值进行初始化DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select();//可以类似dt.Select(&id&10&)之形式达到数据筛选目的
int cl=dt.Columns.C /取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符for(i=0;i&i++)
if(i==(cl-1))//最后一列,加n
colHeaders +=dt.Columns.Caption.ToString() +&n&;
colHeaders+=dt.Columns.Caption.ToString()+&t&;
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//逐行处理数据
foreach(DataRow row in myRow)
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for(i=0;i&i++)
if(i==(cl-1))//最后一列,加n
ls_item +=row.ToString()+&n&;
ls_item+=row.ToString()+&t&;
resp.Write(ls_item);
ls_item=&&;
resp.End();
4、将dataview导出excel
若想实现更加富于变化或者行列不规则的excel导出时,可用本法。
[C#] 纯文本查看 复制代码 public void OutputExcel(DataView dv,string str)
//dv为要输出到Excel的数据,str为标题名称
GC.Collect();
A// = new Application();
int rowIndex=4;
int colIndex=1;
_Workbook xBk;
_Worksheet xSt;
excel= new ApplicationClass();
xBk = excel.Workbooks.Add(true);
xSt = (_Worksheet)xBk.ActiveS
//取得标题
foreach(DataColumn col in dv.Table.Columns)
colIndex++;
excel.Cells[4,colIndex] = col.ColumnN
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignC//设置标题格式为居中对齐
//取得表格中的数据
foreach(DataRowView row in dv)
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
colIndex ++;
if(col.DataType == System.Type.GetType(&System.DateTime&))
excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString(&yyyy-MM-dd&);
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignC//设置日期型的字段格式为居中对齐
if(col.DataType == System.Type.GetType(&System.String&))
excel.Cells[rowIndex,colIndex] = &'&+row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignC//设置字符型的字段格式为居中对齐
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
//加载一个合计行
int rowSum = rowIndex + 1;
int colSum = 2;
excel.Cells[rowSum,2] = &合计&;
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignC
//设置选中的部分的颜色
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//取得整个报表的标题
excel.Cells[2,2] =
//设置整个报表的标题格式
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold =
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
//设置报表表格为最适应宽度
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
//设置整个报表的标题为跨列居中
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossS
//绘制边框
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlT//设置左边线加粗
xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlT//设置上边线加粗
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlT//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlT//设置下边线加粗
//显示效果
excel.Visible=
//xSt.Export(Server.MapPath(&.&)+&&+this.xlfile.Text+&.xls&,SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
xBk.SaveCopyAs(Server.MapPath(&.&)+&&+this.xlfile.Text+&.xls&);
xBk.Close(false, null,null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
GC.Collect();
string path = Server.MapPath(this.xlfile.Text+&.xls&);
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset=&GB2312&;
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为&文件下载/另存为&对话框指定默认文件名
Response.AddHeader(&Content-Disposition&, & filename=& + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader(&Content-Length&, file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = &application/ms-excel&;
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
Response.End();
& &上面的方面,均将要导出的excel数据,直接给浏览器输出文件流,下面的方法是首先将其存到服务器的某个文件夹中,然后把文件发送到客户端。这样可以持久的把导出的文件存起来,以便实现其它功能。
5、将excel文件导出到服务器上,再下载。
二、winForm中导出Excel的方法:
1、方法1:
& &[C#] 纯文本查看 复制代码 SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[&conn&]);
SqlDataAdapter da=new SqlDataAdapter(&select * from tb1&,conn);
DataSet ds=new DataSet();
da.Fill(ds,&table1&);
DataTable dt=ds.Tables[&table1&];
string name=System.Configuration.ConfigurationSettings.AppSettings[&downloadurl&].ToString()+DateTime.Today.ToString(&yyyyMMdd&)+new Random(DateTime.Now.Millisecond).Next(10000).ToString()+&.csv&;//存放到web.config中downloadurl指定的路径,文件格式为当前日期+4位随机数
FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding(&gb2312&));
sw.WriteLine(&自动编号,姓名,年龄&);
foreach(DataRow dr in dt.Rows)
sw.WriteLine(dr[&ID&]+&,&+dr[&vName&]+&,&+dr[&iAge&]);
sw.Close();
Response.AddHeader(&Content-Disposition&, & filename=& + Server.UrlEncode(name));
Response.ContentType = &application/ms-excel&;// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.WriteFile(name); // 把文件流发送到客户端
Response.End();
public void Out2Excel(string sTableName,string url)
Excel.Application oExcel=new Excel.Application();
Workbooks oB
Workbook oB
Worksheet oS
string sFile=&&,sTemplate=&&;
System.Data.DataTable dt=TableOut(sTableName).Tables[0];
sFile=url+&myExcel.xls&;
sTemplate=url+&MyTemplate.xls&;
oExcel.Visible=
oExcel.DisplayAlerts=
//定义一个新的工作簿
oBooks=oExcel.W
oBooks.Open(sTemplate,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing, Type.Missing);
oBook=oBooks.get_Item(1);
oSheets=oBook.W
oSheet=(Worksheet)oSheets.get_Item(1);
//命名该sheet
oSheet.Name=&Sheet1&;
oCells=oSheet.C
//调用dumpdata过程,将数据导入到Excel中去
DumpData(dt,oCells);
oSheet.SaveAs(sFile,Excel.XlFileFormat.xlTemplate,Type.Missing,Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
oBook.Close(false, Type.Missing,Type.Missing);
//退出Excel,并且释放调用的COM资源
oExcel.Quit();
GC.Collect();
KillProcess(&Excel&);
private void KillProcess(string processName)
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打开的进程
foreach (Process thisproc in Process.GetProcessesByName(processName))
if(!thisproc.CloseMainWindow())
thisproc.Kill();
catch(Exception Exc)
throw new Exception(&&,Exc);
2、方法2:
[C#] 纯文本查看 复制代码 protected void ExportExcel()
gridbind();
if(ds1==null)
string saveFileName=&&;
bool fileSaved=
SaveFileDialog saveDialog=new SaveFileDialog();
saveDialog.DefaultExt =&xls&;
saveDialog.Filter=&Excel文件|*.xls&;
saveDialog.FileName =&Sheet1&;
saveDialog.ShowDialog();
saveFileName=saveDialog.FileN
if(saveFileName.IndexOf(&:&)&0) //被点了取消
excelapp.Workbooks.Open
(App.path & 工程进度表.xls)
Excel.Application xlApp=new Excel.Application();
object missing=System.Reflection.Missing.V
if(xlApp==null)
MessageBox.Show(&无法创建Excel对象,可能您的机子未安装Excel&);
Excel.Workbooks workbooks=xlApp.W
Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
string oldCaption=Title_label .Text.Trim ();
long totalCount=ds1.Tables[0].Rows.C
long rowRead=0;
float percent=0;
worksheet.Cells[1,1]=Title_label .Text.Trim ();
//写入字段
for(int i=0;i&ds1.Tables[0].Columns.Ci++)
worksheet.Cells[2,i+1]=ds1.Tables[0].Columns.ColumnN
range=(Excel.Range)worksheet.Cells[2,i+1];
range.Interior.ColorIndex = 15;
range.Font.Bold =
//写入数值
Caption .Visible =
for(int r=0;r&ds1.Tables[0].Rows.Cr++)
for(int i=0;i&ds1.Tables[0].Columns.Ci++)
worksheet.Cells[r+3,i+1]=ds1.Tables[0].Rows[r];
rowRead++;
percent=((float)(100*rowRead))/totalC
this.Caption.Text= &正在导出数据[&+ percent.ToString(&0.00&)
Application.DoEvents();
worksheet.SaveAs(saveFileName,missing,missing,missing,missing,missing,missing,missing,missing);
this.Caption.Visible=
this.Caption.Text= oldC
range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[ds1.Tables[0].Rows.Count+2,ds1.Tables[0].Columns.Count]);
range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexA
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlC
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlT
if(ds1.Tables[0].Columns.Count&1)
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex=Excel.XlColorIndex.xlColorIndexA
workbook.Close(missing,missing,missing);
xlApp.Quit();
} 三、附注:
虽然都是实现导出excel的功能,但在asp.net和winform的程序中,实现的代码是各不相同的。在asp.net中,是在服务器端读取数据,在服务器端把数据以ms-excel的格式,以Response输出到浏览器(客户端);而在winform中,是把数据读到客户端(因为winform运行端就是客户端),然后调用客户端安装的office组件,将读到的数据写在excel
该用户从未签到
不错不错。顶一下
该用户从未签到
呵呵不错支持!
该用户从未签到
呵呵不错支持!
该用户从未签到
楼主,使用OutputExcel()方法之后提示错误,asp.net没有访问权限,但是IIS已经增加了相应【asp.net电脑用户】、【Authenticated Users】、【Users】等仍然没有解决这个问题。其它人怎么就没有这种烦恼呢?Q:F:\OutPutExcel.jpg
该用户从未签到
附件内容。
(145.72 KB, 下载次数: 143)
21:16 上传
TA的每日心情开心前天&10:45签到天数: 519 天[LV.9]以坛为家II
jinzaiya 发表于
楼主,使用OutputExcel()方法之后提示错误,asp.net没有访问权限,但是IIS已经增加了相应【asp.net电脑用 ...
Service用户给权限了吗?
TA的每日心情开心 15:57签到天数: 4 天[LV.2]偶尔看看I
太好了,谢谢站长分享{:soso_e113:}
该用户从未签到
哥们,帮我看下,我的是什么情况、
13:37 上传
点击文件名下载附件
167.41 KB, 下载次数: 769
该用户从未签到
太好了,谢谢站长分享~c# 实现常用文件转换成txt文件代码实现_西西软件资讯
西西软件园多重安全检测下载网站、值得信赖的软件下载站!
→ c# 实现常用文件转换成txt文件代码实现
类型:反编译(译逆工程)大小:7KB语言:中文 评分:5.0
1.pdf DQ txt通^ PDFBox M件,生成txt文件。需要下dPDFBox M件。2.word excell DQtxt直接{用相M件,另存txt。需要注意:2.1 word 文nP],需要{用&&      &object SaveChange =&&&&&&&&&&&&&&& app.Quit(ref SaveChange, ref obj, ref obj);2.2 excell 文nP],需要{用     &&wbk.Close(Type.Missing, Type.Missing, Type.Missing);     & wst =&&&&&&&&&&&&&&& wbk =&&&&&&&&&&&&&&& app.Quit();在打_excell文n的r候,x值2量&app.Visible =//打_的excell不可&app.DisplayAlerts =//不是@示出υ框3.下面是F代a:3.1 建IcDocument接口
public interface IcDocument
void TransformDocument();
}3.2 建操作基 BaseDocument public abstract class BaseDocument
/// &summary&
/// 目宋募A
/// &/summary&
protected string TargetF
/// &summary&
/// 原文件
/// &/summary&
/// &summary&
/// 目宋募
/// &/summary&
protected string T
protected virtual void GetCurrentTarget() {
if (!Directory.Exists(TargetFolder))
Directory.CreateDirectory(TargetFolder);
string fileName =
Guid.NewGuid().ToString()+&.txt&;
Target= TargetFolder + @&\& + fileN
public BaseDocument(string TargetFolder, string source)
this.source =
this.TargetFolder = TargetF
GetCurrentTarget();
}3.3 建 工程 FactoryDocument,根魅氲霓DQ文n後Y,生成不同的子。 public
class FactoryDocument
/// &summary&
/// 得到操作的文n
/// &/summary&
/// &param name=&TargetFolder&&生成的文件A&/param&
/// &param name=&source&&要x取的文件&/param&
/// &returns&&/returns&
public static
IcDocument GetDocoment(string TargetFolder,string source) {
FileInfo file = new FileInfo(source);
IcDocument document =
if (file.Exists)
switch (Path.GetExtension(source).ToUpper())
case &.PDF&:
document = new PdfDocument(TargetFolder, source);
case &.DOC&:
document = new WordDocument(TargetFolder, source);
case &.XLS&:
document = new EexcelDocument(TargetFolder, source);
document = new PdfDocument(TargetFolder, source);
Console.WriteLine(&文件]有找&);
}3.4 建excell操作 EexcelDocument : BaseDocument, IcDocument
public class EexcelDocument : BaseDocument, IcDocument
public EexcelDocument(string TargetFolder, string source)
: base(TargetFolder,source )
#region IcDocument 成T
public void TransformDocument()
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible =
app.DisplayAlerts =
Microsoft.Office.Interop.Excel.Workbook wbk = app.Workbooks.Open(source, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet wst = (Worksheet)wbk.Worksheets[1];
wbk.SaveAs(Target, XlFileFormat.xlUnicodeText,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
XlSaveAsAccessMode.xlNoChange, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing);
wbk.Close(Type.Missing, Type.Missing, Type.Missing);
catch (COMException ex)
Console.WriteLine(ex.Message);
app.Quit();
GC.Collect();
#endregion
}3.5 建word 操作 &WordDocument : BaseDocument, IcDocument
public class WordDocument : BaseDocument, IcDocument
public WordDocument(string TargetFolder, string source)
: base(TargetFolder,source )
#region IcDocument 成T
public void TransformDocument()
Application app = new Application();
Documents Docs = app.D
object obj = Missing.V
object FileName =
Docs.Open(ref FileName, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);
Document ad = app.ActiveD
FileName = T
object FileFormat =
FileFormat = WdSaveFormat.wdFormatT
ad.SaveAs(ref FileName, ref FileFormat, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);
catch (COMException ex)
Console.WriteLine(ex.Message);
object SaveChange =
app.Quit(ref SaveChange, ref obj, ref obj);
GC.Collect();
#endregion
}3.6 建pdf 操作 PdfDocument : BaseDocument,IcDocument public class PdfDocument : BaseDocument,IcDocument
public PdfDocument(string TargetFolder, string source)
: base(TargetFolder,source )
public void pdf2txt(FileInfo file)
PDDocument doc = PDDocument.load(file.FullName);
PDFTextStripper pdfStripper = new PDFTextStripper();
string text = pdfStripper.getText(doc);
StreamWriter swPdfChange = new StreamWriter(Target, false, Encoding.GetEncoding(65001));
swPdfChange.Write(text);
swPdfChange.Close();
#region IcDocument 成T
public void TransformDocument()
FileInfo pdffile = new FileInfo(source);
if (pdffile.Exists)
pdf2txt(pdffile);
Console.WriteLine(&The File is NOT Exist.&);
#endregion
}3.7 在程序中使用
static void Main(string[] args)
IcDocument document = FactoryDocument.GetDocoment(&c:\\temp&, @&C:\Users\Desktop\changes.pdf&);
document.TransformDocument();
document = FactoryDocument.GetDocoment(&c:\\temp&, @&D:\WorkDocuments\201203.xls&);
document.TransformDocument();
06-1812-1104-2701-0703-1408-1101-2201-2201-1701-12
阅读本文后您有什么感想? 已有23人给出评价!
名称大小下载}

我要回帖

更多关于 excel文件转pdf格式 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信