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. MyEclipse中代码格式化后自动换行

    MyEclipse的默认设置里面各种坑人,怎么不方便怎么设置,用户体验差到极点.今天又遇到个问题,按下Ctrl + Shift + F 后,自动格式化后的代码原来只有一行,结果变成了3行,看着都想吐. ...

  2. Dimmer: 通过移动鼠标来改变 LED 的亮度

    原文地址 - https://www.arduino.cc/en/Tutorial/Dimmer 调光器 本例展示了如何通过个人电脑发送数据到 Arduino / Genuino 开发板来控制一个LE ...

  3. Selenium WebDriver 之 PageObjects 模式 by Example

    目录 1. 项目配置 2. 一个WebDriver简单例子 3. 使用Page Objects模式 4. 总结 5. Troubleshooting 6. 参考文档 本篇文章通过例子来阐述一下Sele ...

  4. eclipse提高效率 MAC

    1添加代码提示,在java contentaasist里面加所有字母   2行注释,  块注释mac里是command ctrl加\,鼠标行注释shift+\   3.快速添加注释doc comman ...

  5. 通过创建临时表合并hive小文件

    #!/bin/bash #set -x DB=$1 #获取hive表定义 ret=$(hive -e "use ${DB};show tables;"|grep -v _es|gr ...

  6. double四舍五入,商品金额大小写转换,设置货币的小数位数跟格式输出,进制转化

      1:计算double值四舍五入的方法 对小数数值进行四舍五入,首先应该确认保留小数位, 如果数值的小数精度大于保留小数位,那么开始四舍五入计算.四舍五入的方法非常简单,在所有要丢失精度的小数位中加 ...

  7. js访问xml

    从w3school中获取代码 <html> <head> <script type="text/javascript"> var xmlhttp ...

  8. web 音频文件自动播放(兼容所有浏览器)

    function AudioPerform() { var ua = navigator.userAgent.toLowerCase(); var audiopath = "$!{Templ ...

  9. View & draw

    When an iOS application is launched, it starts a run loop. The run loop’s job is to listen for event ...

  10. Hive删除数据库

    DROP DATABASE是删除所有的表并删除数据库的语句.它的语法如下: DROP DATABASE StatementDROP (DATABASE|SCHEMA) [IF EXISTS] data ...