a helper class for generating xls or csv file

using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace Reuters.ProcessQuality.ContentAuto.Lib
{
public class
XlsOrCsvUtil
{
/// <summary>
/// Generate Xls
Or Csv File
/// </summary>
/// <param
name="path">file path name need to save </param>
///
<param name="dicList">title and body</param>
///
<returns></returns>
public static bool
GenerateXls0rCsv(string path, Dictionary<string, List<string>>
dicList)
{
if (dicList == null || dicList.Count <=
1)//title must exist while no data in file
{

string msg = string.Format("no data need to generate");

//logger.Log(msg, Logger.LogType.LogType_Error);
throw new
Exception(msg);
//return false;
}

using (ExcelApp app = new ExcelApp(false, false))

{
try
{
Workbook
wBook = ExcelUtil.CreateOrOpenExcelFile(app, path);

Worksheet wSheet = wBook.Worksheets[1] as Worksheet;

FillExcel(wSheet, dicList);

app.ExcelAppInstance.AlertBeforeOverwriting = false;

wBook.Save();
//TaskResultList.Add(new
TaskResultEntry(MethodBase.GetCurrentMethod().DeclaringType.FullName.Replace("Ric.Generator.Lib.",
""), "ResultFile", path));
return true;

}
catch (Exception ex)
{

string msg = string.Format("generate XlsOrCsv file error ,msg:{0}",
ex.ToString());
//logger.Log(msg,
Logger.LogType.LogType_Error);
throw new
Exception(msg);
//return false;
}

}
}

private static void FillExcel(Worksheet wSheet, Dictionary<string,
List<string>> dic)
{
int rowCount =
dic.Count;
SetTitle(wSheet, dic.Values.ToList()[0]);

for (int i = 1; i < rowCount; i++)
{

var list = dic.Values.ToList()[i];

for (int j = 0; j < list.Count; j++)

{
((Range)wSheet.Cells[i + 1, j + 1]).NumberFormatLocal =
"@";
wSheet.Cells[i + 1, j + 1] = list[j];

}
}
}

private static void SetTitle(Worksheet wSheet, List<string>
list)
{
for (int i = 0; i < list.Count; i++)

{
((Range)wSheet.Columns[ToName(i),
System.Type.Missing]).ColumnWidth = 20;
wSheet.Cells[1, i +
1] = list[i];
}

//((Range)wSheet.Columns["A:" + ToName(list.Count - 1),
System.Type.Missing]).Font.Name = "Arail";//set style of XlsOrCsv

//((Range)wSheet.Rows[1, Type.Missing]).Font.Bold =
System.Drawing.FontStyle.Bold;
//((Range)wSheet.Rows[1,
Type.Missing]).Font.Color =
System.Drawing.ColorTranslator.ToOle(Color.Black);
}

public static string ToName(int index)
{
if
(index < 0)
throw new Exception("invalid parameter");

List<string> chars = new List<string>();

do
{
if (chars.Count > 0)
index--;
chars.Insert(0, ((char)(index % 26 + (int)‘A‘)).ToString());

index = (int)((index - index % 26) / 26);
}

while (index > 0);

return String.Join(string.Empty, chars.ToArray());

}
}
}

a helper class for generating xls or csv file,布布扣,bubuko.com

时间: 2024-10-25 09:03:36

a helper class for generating xls or csv file的相关文章

C# - CSV file reader

// -------------------------------------------------------------------------------------------------------------------- // <summary> // Defines the CSVFileReader type. // </summary> // ----------------------------------------------------------

解决Jmeter Non GUI运行时报“...ensure the jmeter .save.saveservice.* properties are the same as when the CSV file was created or the file may be read incorrectly”错误的问题

错误信息: File 'xxx.jtl' does not contain the field names header, ensure the jmeter.save.saveservice.* properties are the same as when the CSV file was created orthe file may be read incorrectly 解决: jmeter.properties配置文件 jmeter.save.saveservice.output_fo

[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V

Hello everyone, this is the third post of the series. .   Background =============== In my solution, I have 15 Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Withou

How to handle csv file using python

As i need to read something from a csv file using python.  I got something and put it here. Module: csv import csv FILE_FULL_PATH = 'D:\\Work\\script\\My Script\\Book1.csv' def f(): with open(FILE_FULL_PATH,'rb') as csvfile: for row in csv.reader(csv

Powercli随笔 - PowerCLI script to sequentially Storage vMotion VMs from a CSV File

PowerCLI script to sequentially Storage vMotion VMs from a CSV File This is a PowerCLI script that I use to Storage vMotion (s/vmotion) VMs from an input file (CSV File). This helps me evacuate VMs from a datastore that will be decommissioned and thi

xls与csv文件区别?

xls 文件就是Microsoft excel电子表格的文件格式.CSV是最通用的一种文件格式,它可以非常容易地被导入各种PC表格及数据库中. 此文件,一行即为数据表的一行.生成数据表字段用半角逗号隔开.CSV是文本文件,用记事本就能打开,XLS是二进制的文件只有用EXCEL才能打CSV(以逗号分隔)CSV (*.csv) 文件格式只能保存活动工作表中的单元格所显示的文本和数值.工作表中所有的数据行和字符都将保存.数据列以逗号分隔,每一行数据都以回车符结束.如果单元格中包含逗号,则该单元格中的内

phpexcel读取excel的xls xlsx csv格式

我之前写过一篇PHP读取csv文件的内容 上代码index.php <?php /** * * @author XC * */ class Excel { public $currentSheet; public $filePath; public $fileType; public $sheetIndex=0; public $allColumn; public $allRow; public function initialized($filePath) { if (file_exists(

Csharp--Read Csv file to DataTable

在网上找的资料都不怎么好使,许多代码一看就知道根本没有考虑全面. 最后找到一个好用的,在codeplex上,这位老兄写成了一个framework,太重了. http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader 确实挺好用的. 我没耐下性子看他的实现,自己尝试写了如下的代码来完成了阅读csv. 参照:http://msdn.microsoft.com/en-us/library/ae5bf541%28v=vs.90%29.aspx 只

SQL Script for select data from ebs and make a csv file to FTP

DECLARE CURSOR cur_lcy_test IS SELECT rcta.customer_trx_id, rcta.trx_number, rcta.trx_date FROM ra_customer_trx_all rcta WHERE rcta.customer_trx_id = 11993; -- rec_lcy_test cur_lcy_test%ROWTYPE; -- w_csv_line_num NUMBER := 0; TYPE tbl_varchar2 IS TAB