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. 原生js+本地储存登录注册

    //简易操作,只能当前页面储存一个username和password. <!DOCTYPE html> <html> <head> <meta charset ...

  2. Redis 简单命令

    1. 新增 set keyName "keyValue" 2. 获取 get keyName 查看所有Key keys * 3. 删除 //删除当前数据库中的所有Key flush ...

  3. vue.js组件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. GUI(图形用户界面)

    ylbtech-Miscellaneos:GUI(图形用户界面) A,返回顶部 1, 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显 ...

  5. CRM 2016 子表单中N:1关系 字段要求与新建时的关系

    父表单在新建子表单项时弹出的窗口和 子表单的N:1关系是有关系的.说白了就是子表单窗体上的父表单字段是不是必填项. 关系如下: 1 非必填项 点击子表单的"+"号时,会出现look ...

  6. JavaScript的toString()和valueof()方法

    toString()方法: 函数:函数 (function(){}).toString(); //返回"function(){}" typeof((function(){}).to ...

  7. 两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz

    1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 import java.util.TimerTask; public class EmailReportT ...

  8. FreeBSD从零开始---安装后配置(一)

    一.安装后配置   上次我们说到FreeBSD的安装,这次我们说FreeBSD安装后的配置和简单优化方法.   安装完BSD只是服务器提供服务这条万里长征路的开始,还需要一些基本的设定和优化.不过实际 ...

  9. 尚学堂Spring视频教程(二):Spring控制反转

    用Spring来实现IOC 在上节中我们自定义了一个接口BeanFactory和类ClassPathXmlApplicationContext来模拟Spring,其实它们在Spring中确实是存在的, ...

  10. 说说Timing这回事(转载)

    本文原始位置:FPGANotes Blog http://wiki.fpganotes.com/doku.php/ise:timing:my_summary Intro 问:一个FPGA设计项目需要用 ...