/// <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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.