using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel; namespace WinFromAPP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} /// <summary>
/// 将Csv文件转换为XLS文件
/// </summary>
/// <param name="FilePath">文件全路路径</param> /// <returns>返回转换后的Xls文件名</returns>
public static string CSVSaveasXLS(string FilePath)
{
QuertExcel();
string _NewFilePath = ""; Excel.Application excelApplication;
Excel.Workbooks excelWorkBooks = null;
Excel.Workbook excelWorkBook = null;
Excel.Worksheet excelWorkSheet = null; try
{
excelApplication = new Excel.ApplicationClass();
excelWorkBooks = excelApplication.Workbooks;
excelWorkBook = ((Excel.Workbook)excelWorkBooks.Open(FilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value));
excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets[];
excelApplication.Visible = false;
excelApplication.DisplayAlerts = false;
_NewFilePath = FilePath.Replace(".csv", ".xls");
excelWorkBook.SaveAs(_NewFilePath, Excel.XlFileFormat.xlAddIn, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excelWorkBook.Close();
QuertExcel();
// ExcelFormatHelper.DeleteFile(FilePath);
//可以不用杀掉进程QuertExcel(); GC.Collect(System.GC.GetGeneration(excelWorkSheet));
GC.Collect(System.GC.GetGeneration(excelWorkBook));
GC.Collect(System.GC.GetGeneration(excelApplication)); }
catch (Exception exc)
{
throw new Exception(exc.Message);
} finally
{
GC.Collect();
} return _NewFilePath;
} /// <summary>
/// 将xls文件转换为csv文件
/// </summary>
/// <param name="FilePath">文件全路路径</param>
/// <returns>返回转换后的csv文件名</returns>
public static string XLSSavesaCSV(string FilePath)
{
QuertExcel();
string _NewFilePath = ""; Excel.Application excelApplication;
Excel.Workbooks excelWorkBooks = null;
Excel.Workbook excelWorkBook = null;
Excel.Worksheet excelWorkSheet = null; try
{
excelApplication = new Excel.ApplicationClass();
excelWorkBooks = excelApplication.Workbooks;
excelWorkBook = ((Excel.Workbook)excelWorkBooks.Open(FilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value));
excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets[];
excelApplication.Visible = false;
excelApplication.DisplayAlerts = false;
_NewFilePath = FilePath.Replace(".xls", ".csv"); // excelWorkSheet._SaveAs(FilePath, Excel.XlFileFormat.xlCSVWindows, Missing.Value, Missing.Value, Missing.Value,Missing.Value,Missing.Value, Missing.Value, Missing.Value);
excelWorkBook.SaveAs(_NewFilePath, Excel.XlFileFormat.xlCSV, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
QuertExcel();
//ExcelFormatHelper.DeleteFile(FilePath);
}
catch (Exception exc)
{
throw new Exception(exc.Message);
}
return _NewFilePath;
} /// <summary>
/// 删除一个指定的文件
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <returns></returns>
public static bool DeleteFile(string FilePath)
{
try
{
bool IsFind = File.Exists(FilePath);
if (IsFind)
{
File.Delete(FilePath);
}
else
{
throw new IOException("指定的文件不存在");
}
return true;
}
catch (Exception exc)
{
throw new Exception(exc.Message);
} } /// <summary>
/// 执行过程中可能会打开多个EXCEL文件 所以杀掉
/// </summary>
private static void QuertExcel()
{
Process[] excels = Process.GetProcessesByName("EXCEL");
foreach (var item in excels)
{
item.Kill();
}
} private void btnConvert_Click(object sender, EventArgs e)
{
//CSVSaveasXLS(textBox1.Text); XLSSavesaCSV(textBox1.Text);
}
}
}
原文转载自:http://www.cnblogs.com/junjie94wan/archive/2013/05/23/3094483.html

C# .csv文件转为Excel格式;Excel格式转换为.csv的更多相关文章

  1. SSIS 中将csv 文件批量导出到excel 文件,并设置excel 文件中某些列的data column format 为Text

    csv 文件是文本文件类型,但是打开csv 文件后(默认使用本地已经安装的excel 来打开excel 文件),默认显示出来的是general 类型(column data format)的数据, 这 ...

  2. C# CSV文件的导入导出以及datatable转化csv

    1.csv文件写入操作 首先,创建datatable对象: DataTable dt = new DataTable(); 添加列: dt.Columns.Add("name", ...

  3. 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法

    1. 转换成CSV文件: http://www.dotnetgallery.com/lab/resource93-Export-to-CSV-file-from-Data-Table-in-Aspne ...

  4. Spring-Batch将CSV文件转为XML文件

    1 介绍 用Spring Batch实现一个简单的需求,将csv文件转换成xml文件. csv文件如下:record.csv username, user_id, transaction_date, ...

  5. java将Excel文件(xlsx,xls)转换为csv文件

    http://blog.csdn.net/bryan__/article/details/40715309

  6. 把一个文件夹下的多个csv文件合并到一个excel的多个sheet

    #!/usr/bin/env python3 # -*- coding: UTF-8 -*- import pandas as pd import os import re if __name__ = ...

  7. 保存数据到csv文件报错:Permission denied: './train_data.csv'

    如果你此前已经输出,创建了文件,很有可能是你打开了此文件,导致写入不进去报错,关掉文件重新运行程序即可!

  8. python输出excel能够识别的utf-8格式csv文件

    http://blog.csdn.net/azhao_dn/article/details/16989777 可能大家都遇到过,python在输出的csv文件中如果有utf-8格式的中文,那么在使用e ...

  9. 用pandas库修改excel文件里的内容,并把excel文件格式存为csv格式,再将csv格式改为html格式

    假设有Excel文件data.xlsx,其中内容为: ID  age  height     sex  weight张三   1   39     181  female      85李四   2  ...

随机推荐

  1. 远程桌面不能连接,提示awgina.dll取代错误的解决办法

    远程桌面不能连接,错误提示:您不能初始化一个远程桌面连接,因为在远程计算机上的windows登录软件被不兼容的软件c:\windows\system32\awgina.dll取代,如下图所示: 原因: ...

  2. 百度地图API地理位置和坐标转换

    1.由地名(省份.城市.街道等)得到其对应的百度地图坐标: http://api.map.baidu.com/geocoder/v2/?output=json&ak=你从百度申请到的Key&a ...

  3. java和h5 canvas德州扑克开发中(二)

    德州扑克网页源码在github上分享 https://github.com/lxr1907/pokers 感兴趣的可以上去看下. 1.通讯使用websocket,主要在message.js中. 2.用 ...

  4. [HTML] IE=edge,chrome=1的META标签详解

    文件兼容性用于定义让IE如何编译你的网页.此文件解释文件兼容性,如何指定你网站的文件兼容性模式以及如何判断一个网页该使用的文件模式. meta信息中常有这么一句: <meta content=& ...

  5. 【转载】windows平台安装nodejs过程

    第一步 下载nodejs 访问https://nodejs.org/en/download/url 根据您的系统是32位或64位,按照文字说明进行下载 第二步 安装nodejs 双击运行nodejs的 ...

  6. 安卓通用shell大全

    一.[什么是shell] Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口.它是命令语言.命令解释程序及程序设计语言的统称.shell是用户和Linux内核之间的接口程序,如 ...

  7. iisnode托管node

    1.官方下载iisnode https://github.com/tjanczuk/iisnode 2.安装完成后在IIS中可见

  8. DVD管理器集合版

    利用所学的集合写出的DVD管理系统,运用到了所学到集合基础. import java.text.ParseException; import java.text.SimpleDateFormat; i ...

  9. WIn2003的IIS6解决IE11登录问题。

    一键批处理文件下载:http://download.csdn.net/detail/hospp/6850835 1.打开文件夹C:\Windows\Microsoft.NET\Framework\v4 ...

  10. 关于Delphi中多线程传递参数的简单问题

    http://bbs.csdn.net/topics/390513469/ unit uThread; interface uses Classes; type Th = class(TThread) ...