/// <summary>
/// 将取得的结果写入excel
/// </summary>
/// <param name="result">结果</param>
/// <param name="fileName">文件名称</param>
/// <param name="rowNum">行数</param>
private void writeToExcel(List<Dictionary<string, DataTable>> result, string fileName, int totalNum)
{
//创建Excel对象
Excel.Application excelApp = new Excel.ApplicationClass();
//新建工作簿
Excel.Workbook workBook = excelApp.Workbooks.Add(true);
List<string> newQuName = getLocation();
//记录每个区的最大行数(即每个区中查询的结果表中记录数最多的)
int[] maxRowsNum = new int[newQuName.Count];
//创建工作表
for (int i = 0; i < newQuName.Count - 1; i++)
{
excelApp.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
maxRowsNum[i] = 0;
}
bool isNewLine = false;
//更改名称
//循环更改每个区的excel文件
for (int i = 1; i <= newQuName.Count; i++)
{
//找到该区对应的工作表
Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.Sheets[i];
string name = newQuName[i - 1];
workSheet.Name = name;
int colNum = 1;
List<string> headTitle = new List<string>();
for (int k = 0; k < result.Count; k++)
{
Dictionary<string, DataTable> tempDic = result[k];
//没有数据,则跳过
if (tempDic.Count == 0)
{
continue;
}
isNewLine = false;
if (tempDic.ContainsKey(name))
{
DataTable table = tempDic[name];
for (int x = 0; x < table.Columns.Count; x++)
{
string colName = table.Columns[x].ColumnName;
headTitle.Add(colName);
colNum++;
isNewLine = true;
}
//记录每个区的最大行数(即每个区中查询的结果表中记录数最多的)
if (table.Rows.Count > maxRowsNum[i - 1])
{
maxRowsNum[i - 1] = table.Rows.Count;
}
}
if (isNewLine)
{
headTitle.Add("");
colNum++;
}
}
//一次性写入
string beginNum = "A1";
char endChar = (char)(65 + colNum - 2);
string endNum = endChar + "1";
Excel.Range rowRange = workSheet.get_Range(beginNum, endNum);
string[] rowValuesArr = headTitle.ToArray();
rowRange.Value2 = rowValuesArr;
//自动列宽
workSheet.Cells.Columns.AutoFit();
//设置居中
workSheet.Cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
}
//循环读取每个区的信息
for (int g = 0; g < newQuName.Count; g++)
{
Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.Sheets[g + 1];
string name = newQuName[g];
//读取每行信息,每行都是从A开始,到 A+该行个数
//第一行从A2开始,接下来A3,依次递增
//行 A+ (rowNums +2 ) 列 (CHAR(A+rowNums))+(rowNums +2 )
int rowNums = 0;
//每行列数
int colNum = 1;
while (rowNums <= maxRowsNum[g])
{
colNum = 1;
List<string> rowValues = new List<string>();
for (int i = 0; i < result.Count; i++)
{
isNewLine = false;
if (result[i].Count > 0)
{
Dictionary<string, DataTable> tempDic = result[i];
if (tempDic.ContainsKey(name))
{
DataTable table = tempDic[name];
DataRow row = null;
try
{
row = table.Rows[rowNums];
for (int x = 0; x < table.Columns.Count; x++)
{
string content = row[x].ToString();
rowValues.Add(content);
colNum++;
isNewLine = true;
}
}
catch (IndexOutOfRangeException)
{
for (int b = 0; b < table.Columns.Count; b++)
{
rowValues.Add("");
colNum++;
isNewLine = true;
}
}
}
}
if (isNewLine)
{
rowValues.Add("");
colNum++;
}
}
//一次性写入一行,数据写入excel速度较快
string beginNum = "A" + (rowNums + 2);
char endChar = (char)(65 + colNum - 2);
string endNum = endChar + "" + (rowNums + 2);
Excel.Range rowRange = workSheet.get_Range(beginNum, endNum);
string[] rowValuesArr = rowValues.ToArray();
rowRange.Value2 = rowValuesArr;
rowNums++;
}
//自动列宽
workSheet.Cells.Columns.AutoFit();
//设置居中
workSheet.Cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
}
//保存文件
workBook.SaveCopyAs(fileName);
workBook.Saved = true;
workBook.Close(false, fileName, false);
//excelApp.Quit();
QuitExcel(ref excelApp);
}
C#写excel
最新推荐文章于 2022-01-06 10:38:27 发布