写PDF:

写出 pdf 用到ICSharpCode.SharpZipLib.dll 、 itextsharp.dll (网上下)


 
using iTextSharp.text;
using iTextSharp.text.pdf;
//C#处理excel、pdf、access..
    
//表格邊框寬度
    private float myTableBorderWidth = 0.01f;
    
//使用字體
    private string myFontDir = @"C:\WINDOWS\Fonts\MINGLIU.TTC,0";
//C#处理excel、pdf、access.
    BaseFont bfSun = BaseFont.createFont(this.myFontDir, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
    iTextSharp.text.Font documentFont 
= new iTextSharp.text.Font(bfSun,4,iTextSharp.text.Font.NORMAL);
    Document tmpDocument 
= new Document(PageSize.A4.rotate());//横的
    tmpDocument.setMargins(6f,6f,20f,20f);//设置下边距
    PdfWriter pdfwriter = PdfWriter.getInstance(tmpDocument, new FileStream(this.outputdir+pdffilename + ".pdf", FileMode.Create));
    
//设置页脚
    HeaderFooter footer = new HeaderFooter(new Phrase("Page ", documentFont), true);
    footer.Alignment 
= Element.ALIGN_CENTER;
    footer.Border 
= iTextSharp.text.Rectangle.NO_BORDER;
    tmpDocument.Footer 
= footer; 
    tmpDocument.Open();
    Paragraph mytitle 
= new Paragraph("标题123321\n\n", documentFont);
    mytitle.Alignment 
= Element.ALIGN_CENTER;
    tmpDocument.Add(mytitle);
    
//略过根据数据量判断是否分页的代码C#处理excel、pdf、accessC#处理excel、pdf、accessC#处理excel、pdf、access
    Table aTable = new Table( 24, (hasMultiPages?this.onepageContentNum:totalRowNum) + 1 );
    aTable.Border 
= iTextSharp.text.Rectangle.LEFT | iTextSharp.text.Rectangle.BOTTOM;
    aTable.BorderWidth 
= this.myTableBorderWidth;
    aTable.Widths 
= new float[] {   12f, 12f, 15f, 18f, 12f, 12f, 12f, 15f, 18f, 12f,
        15f, 12f, 18f, 18f, 15f, 12f, 18f, 18f, 15f, 15f,
        15f, 15f, 12f, 12f};
    aTable.AutoFillEmptyCells 
= true;//设置自动填充
    
//设置表格中内容
    Cell tmpCell = new Cell(new Paragraph(tmpCellText+"\n", documentFont));
    tmpCell.HorizontalAlignment 
= Element.ALIGN_CENTER;
    tmpCell.Border 
= iTextSharp.text.Rectangle.TOP | iTextSharp.text.Rectangle.RIGHT;
    tmpCell.BorderWidth 
= this.myTableBorderWidth;
    aTable.addCell(tmpCell, 
new Point(i, pos));
    
//C#处理excel、pdf、access.
    tmpDocument.Add(aTable);
    tmpDocument.newPage();
//适当的时候调这句翻页
    
//pdf打完收工
    tmpDocument.Add(aTable);
 

读ACCESS:
;
    using (OleDbConnection con = new OleDbConnection(this.strCon))
    {
        con.Open();
        OleDbCommand cm 
= new OleDbCommand(strSql, con);
        OleDbDataReader reader 
= cm.ExecuteReader();
    
while (reader.Read())
        {
        
string strTmpIDCard = reader[0].ToString();
        
//C#处理excel、pdf、access
    }
    
//读完收工
    reader.Close();
    con.Close();

读excel:
引入Microsoft.Office.Interop.Excel.dll先 
 Microsoft.Office.Interop.Excel;

    Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
    
object missing = Type.Missing;
    Microsoft.Office.Interop.Excel.Workbook theWorkbook 
= ExcelObj.Workbooks.Open(
         
this.xlwfileurl, missing, missing, missing, missing, missing, missing, missing,
         missing, missing, missing, missing, missing, missing, missing);
    Microsoft.Office.Interop.Excel.Sheets sheets 
= theWorkbook.Worksheets;
    Microsoft.Office.Interop.Excel.Worksheet datasheet 
= null;
    
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets) {
        
if (sheet.Name == textBoxSheetName) { datasheet = sheet; break; }
    }
    
if (null == datasheet)
    {
        MessageBox.Show(
this"没有名称为" + textBoxSheetName + "的Sheet.");
        
return;
    }
    
int totalrow_num = datasheet.Rows.Count;
    
//读死格式的文件,头不要了
    for (int i = 2; i <= totalrow_num; i++) {
        
//读个值出来
    Object obj_id_card = datasheet.get_Range(datasheet.Cells[i, 3], datasheet.Cells[i, 3]).Value2 ;
    Microsoft.Office.Interop.Excel.Range range 
= datasheet.get_Range(datasheet.Cells[i,2], datasheet.Cells[i, 25]);
    }
    
//执行那么多释放资源,还是锁着读的那个excel文件呢~NND
    datasheet = null;
    sheets 
= null;
    theWorkbook 
= null;
    ExcelObj 
= null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
 


 

相关文章:

  • 2022-12-23
  • 2022-02-10
  • 2022-12-23
  • 2021-09-20
  • 2021-09-17
  • 2021-10-21
  • 2022-12-23
  • 2021-12-31
猜你喜欢
  • 2021-10-28
  • 2022-12-23
  • 2022-01-20
  • 2021-08-22
  • 2021-08-21
  • 2022-12-23
  • 2021-06-14
相关资源
相似解决方案